Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Basement
    Posts
    88
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Localstorage ID problem.

    It looks like this is not working:

    HTML:

    Code:
    <div id="zaal3"style="position: relative; width: 450px; height: 300px; border: 1px solid black" id="allseats"/>
    JS:
    Code:
    window.onload = function() {
       if(window.localStorage && window.localStorage.seats) {
         var theArray = JSON.parse(window.localStorage.seats);
         var allestoelen = document.getElementById('allseats').getElementsByTagName('div');
         for(i=0; i<allestoelen.length; i++) {
           if(theArray[i]===1) allestoelen[i].className="blocked";
         }
       }
    };
    
    function storeIt() {
      var allestoelen = document.getElementById('allseats').getElementsByTagName('div');
      //alert(allestoelen.length);
      var theArray = [];
      for(i=0; i<allestoelen.length; i++) {
        if(allestoelen[i].className == "zwart") {
          theArray.push(1);
        } else {
          theArray.push(0);
        }
      }
      var jsonArray = JSON.stringify(theArray);
      window.localStorage.seats = jsonArray;
      console.log(jsonArray);
      return false;
    }
    Someone told me having 2 id's won't work (Id zaal3 & Id allseats in 1 line).
    I tried changing 'allseats' in JS to 'zaal3' but it dont seem to be working. What am i doing wrong?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,479
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Yo can't give one element two ids. Also that div is self closing which means that it can't contain any other divs for the getElementsBytagName to find.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Oct 2012
    Location
    Basement
    Posts
    88
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I give up lol

  • #4
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post
    /*ID's are single use and are only applied to one element

    They are used to identify a single element*/

    //Classes can be used more than once.

    <div class="soso poso">

    soso class 1

    poso class 2


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •