www.webdeveloper.com
Results 1 to 7 of 7

Thread: getElementByName

  1. #1
    Join Date
    Jan 2009
    Posts
    15

    getElementByName

    I cannot figure out why this seemingly simple code won't work, the "else if" line.
    Code:
    <script type="text/javascript">
    <!--
    function checkKeywords(x)
    {
       if(x==1) { //
           document.getElementById('Lights').checked = true;
           document.getElementById('Lighting').checked = true;
           document.getElementById('Light').checked = true;
           document.getElementById('Lamp').checked = true;
           document.getElementById('Table').checked = true;
       } else if(x==2) {
           document.getElementsByName('keyword').checked = false;
       }
    }
    
    // -->
    </script>
    
    <label><input type="radio" name="Category" value="table_lamp" onclick="checkKeywords(1);">Table Lamp</label><br>
    <label><input type="radio" name="Category" value="floor_lamp" onclick="checkKeywords(2);">Floor Lamp</label>
    <br><br>
    <label><input type="checkbox" name="keyword" value="Lights" id="Lights">Lights</label><br>
    <label><input type="checkbox" name="keyword" value="Lighting" id="Lighting">Lighting</label><br>
    <label><input type="checkbox" name="keyword" value="Light"  id="Light">Light</label>

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Because it returns an array and you would need to loop through each one.

    Eric

  3. #3
    Join Date
    Jan 2009
    Posts
    15
    Hi Eric,
    thank you so much. Sorry to be such a newbie, but could you show me how that would look?

  4. #4
    Join Date
    Oct 2006
    Posts
    939
    } else if(x==2) {alert(document.getElementsByName('keyword').length)

  5. #5
    Join Date
    Jan 2009
    Posts
    15
    Justin, thanks. It gets me an alert window that gives me "3", but how do I use that to uncheck all the checkboxes?

  6. #6
    Join Date
    Oct 2006
    Posts
    939
    Hmm, I don't know why A1ien51 didn't take care of you this, its simple.

    } else if(x==2) { var k = document.getElementsByName('keyword');
    for(i=0;i<k.length;i++){ k[i].checked = false;}
    Last edited by justinbarneskin; 09-01-2009 at 12:16 PM. Reason: not tested

  7. #7
    Join Date
    Jan 2009
    Posts
    15
    thanks again, Justin!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles