0

So, I'm working on teaching myself jQuery with the help of some books, and I'm currently stumped by a seemingly simple problem. Why does the value of $('div.foo').length evaluate to 0? A div element exists in the page, so why doesn't the $('div.foo').length evaluate to 1?

<!DOCTYPE html>
<html>
<head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript">
        </script>
        <script type="text/javascript">
            document.write($(div.foo).length);
            </script>
    <body>
            <div class="foo"></div>
    </body>
</html>

5 Answers 5

2
 document.write($(div.foo).length) 

should be

 document.write($("div.foo").length);

And also, your scipt will be run before the DOM is loaded, so try encapsulating the JavaScript in jQuery's $(document).ready - see http://docs.jquery.com/Tutorials%3AIntroducing_%24(document).ready()

Sign up to request clarification or add additional context in comments.

Comments

1

You are missing the quotes

document.write($("div.foo").length);

Edit:

Brad makes a good point. You should wrap this call in document.ready

$(document).ready(function() { 
    document.write($("div.foo").length);
});

2 Comments

Though you're right about quotes, the DOM isn't fully loaded yet. It still won't see the element.
@Brad: Edited to add more info. Thanks
0

Simple: the DOM isn't loaded yet.

You have two options:

  1. place the code just before the </body> tag so the document elements have been loaded and, thus, jQuery has visibility of the object.
  2. Use $(document).ready(function(){ ...code... }) (or short-hand $(function(){ ...code... }) so the code within the braces only executes after the document is loaded.

Try these two on for size:

re-ordering executon:

<html>
  <head>
    <script src="jquery.js"></script>
  </head>
  <body>
    <div class="foo">
    <script>
      alert($('.foo').length);
    </script>
  </body>
</html>

wait for loaded DOM*

<html>
  <head>
    <script src="jquery.js"></script>
    <script>
      $(document).ready(function(){
        alert($('.foo').length);
      });
    </script>
  </head>
  <body>
    <div class="foo">
  </body>
</html>

Comments

0

you have to move this code after dom elements are created, by moving it at bottom before body tag. Currently you are try to access elements even before they are created in DOM.

   <script type="text/javascript">
        document.write($('div.foo').length);
   </script>

or

Run your code after DOM is ready i.e. all elements are created in DOM.

//this piece of code can be placed even before elements are created
    $(function(){
    //this code here, will run after DOM is ready
    });

Comments

0

DOM is not loaded. Use

    $(document).ready(function() {
          alert($('div.foo').length); 
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.