skip to Main Content

I am making a to-do list in javascript but I am getting an error: Uncaught ReferenceError: itemsjson is not defined.

I Have set my get.item null after that added .push
Please can anyone solve the issue
thanks

Note: i’m using Bootstrap for HTML

<!doctype html>
<html lang="en">
<div class="form">
    <label for="formGroupExampleInput" class="form-label">Example label</label>
    <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
  
      <label for="formGroupExampleInput2" class="form-label">Another label</label>
    <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder"> <br>

    <button type="submit" id="add" class="btn btn-primary">Sign in</button>

  </div>

  <table class="table">
    <thead>
      <tr>
        <th scope="col">Item</th>
        <th scope="col">Item Description</th>
        <th scope="col">Actions</th>
      </tr>
    </thead>
    <tbody>
      <tr>
  
        <td>Parvinder</td>
        <td>Hello, Parvinder how are you?</td>
        <td><button type="button" class="btn btn-primary btn-sm">Small button</button></td>
      </tr>
    </tbody>
  </table>
<script>
    tatti = document.getElementById("add");
    tatti.addEventListener("click", () => {
      console.log("updating");

      tit = document.getElementById('formGroupExampleInput').value;
      desc = document.getElementById('formGroupExampleInput2').value;
      if (localStorage.getItem(itemsjson)==null){
      itemjsonArray = [];
      itemjsonArray.push([tit,desc]);
      localStorage.setItem('itemjson', JSON.stringify(itemsjsonArray));
      }
    })
  </script>

  </center>
</html>

2

Answers


  1. if (localStorage.getItem(itemsjson)==null){
    

    will search for valiable name itemsjson

    it should be like this

    localStorage.getItem('itemsjson')
    
    Login or Signup to reply.
  2. You forget to put quotes in if(localStorage.getItem(itemsjson) ==null).

    <script>
        tatti = document.getElementById("add");
        tatti.addEventListener("click", () => {
          console.log("updating");
    
          tit = document.getElementById('formGroupExampleInput').value;
          desc = document.getElementById('formGroupExampleInput2').value;
          // Change to this line
          if (localStorage.getItem("itemsjson")==null){
              itemjsonArray = [];
              itemjsonArray.push([tit,desc]);
              localStorage.setItem('itemjson', JSON.stringify(itemsjsonArray));
          }
        })
      </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search