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
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Thumbs up querySelectorAll() NodeList Usage

    Code:
    function themeOne() {
    
    	var thm = document.querySelectorAll("div.articles");
    	for( i = 0; i < thm.lenght; i++)
    	document.thm[i].style.border = "5px solid red";
    
    }
    So all I'm trying to do here is to change the styles of the div's that have a class attribute with the value "articles". The function call works and a NodeList is created and placed in the variable thm. But before the first pass of the for loop, firebug is telling me that thm is undefined.

    You can see in the picture below that thm was infact defined and the NodeList was properly created and placed in the variable.

    Can someone tell me why thm is being returned undefined?


    querySlectorProblem by FutureWebDev, on Flickr
    Last edited by FutureWebDev; 05-06-2012 at 06:49 AM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You haven't defined thm.lenght - possibly you meant to type thm.length
    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.

  • Users who have thanked felgall for this post:

    FutureWebDev (05-06-2012)

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    You haven't defined thm.lenght - possibly you meant to type thm.length
    LOL. Well at least I got some good practice at posting. Thanks for being my ritalin replacement.

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    This Doesn't Work
    Code:
    function themeOne() {
    
    	var thm = document.querySelectorAll("div.articles");
    	for( i = 0; i < thm.lenght; i++)
    	document.thm[i].style.border = "5px solid red";
    
    }
    This Does Work
    Code:
    function themeOne() {
    
    	var thm = document.querySelectorAll("div.articles");
    	for( i = 0; i < thm.length; i++)
    	document.thm[i].style.border = "5px solid red";
    
    }
    Thanks Felgall


  •  

    Tags for this Thread

    Posting Permissions

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