-2

I'm trying to count number of divs with a certain class, but .length keeps returning 0. What could be going wrong?

    var keyspressed 
    keyspressed = $('.key').length;
    console.log(keyspressed)

I have a very simple document structured like this:

    <body>
        <div class="keyboard">
            <div class="key"> (many of these)

Where am I going wrong? thanks!

  • 2
    Is jQuery loaded on the page? Is your script running before the DOM has finished loading? Can you show the whole script? – pete Oct 31 '17 at 13:07
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) If there were `.key` elements when you ran the code shown, they would be found, so there's clearly something else going on (perhaps you're running the code before the elements exist). Please update your question with a **runnable** [mcve] demonstrating the problem, using Stack Snippets (the `[<>]` toolbar button). – T.J. Crowder Oct 31 '17 at 13:07
  • http://jsfiddle.net/ky47etre/ it seems to work fine for me. Is there any other code you are using? – Wouter Bouwman Oct 31 '17 at 13:07
  • @Pete jQuery is loaded on the page (because the output is `0`, not `$ is undefined`). So it's just because the script runs before the HTML is ready. – Jeremy Thille Oct 31 '17 at 13:13
  • Thanks for the help guys. Sorry about the duplicate question, the link @T.J.Crowder linked below already answers a similar question (didn't recognize it, only just started learning javascript/jquery). Should I delete this question or will it be deleted automatically? – J. Stockland Oct 31 '17 at 13:22

1 Answers1

1

Early $('.key') call is the only cause of the issue that I see. So you need to wait for DOM is ready:

$(function(){
   var keyspressed  = $('.key').length;
   console.log(keyspressed);
});
dhilt
  • 13,532
  • 6
  • 48
  • 67
  • 1
    I don't why the downvotes.. Maybe that is the solution.. – Mosh Feu Oct 31 '17 at 13:09
  • @MoshFeu: It's the **maybe**. Guesses aren't answers, and `ready` is unnecessary in nearly all cases. If this *is* the solution, then the question is just a duplicate of http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element. Either way, the answer is not useful. – T.J. Crowder Oct 31 '17 at 13:09
  • People are suggesting solution all the time. Not of all answers are solve the problem. But this is my opinion.. – Mosh Feu Oct 31 '17 at 13:11
  • If it is the solution (and I believe it is), it didn't deserve a downvote. If it just not useful because the question is a duplicate, it still didn't deserve a downvote. – Jeremy Thille Oct 31 '17 at 13:12
  • 1
    Thanks, this solved it! – J. Stockland Oct 31 '17 at 13:12
  • 2
    @J.Stockland Very good :) Please accept the answer so it will help to other peoples. – Mosh Feu Oct 31 '17 at 13:14
  • I will, when I can in 4 minutes. I guess the link by @T.J.Crowder answers my question as well, although I wouldn't have recognized it from the title. – J. Stockland Oct 31 '17 at 13:16
  • @JeremyThille: Answering with the obvious, dramatically-duplicated duplicate is actively harmful, because it prevents the OP from deleting the question. Sometimes more duplicates are useful, but this issue is **entirely** covered and further duplicates are just more noise. – T.J. Crowder Oct 31 '17 at 13:16
  • 1
    I agree, but in that case, the question needs to be downvoted. The dude who gave the correct answer does not deserve to lose some reputation because of that. – Jeremy Thille Oct 31 '17 at 13:18