JavaScript show() on nth element in jQuery show div jquery,show div jquery animation,show div jquery

First time asker. Trying to make a hangman game. When I get keyboard input and match it to a letter in the word, my alert pops. But the execution of the jquery code to show the nth span element inside the .word class does not. Is this a scoping issue? What am I missing?

Thanks

$(function(){
  words = ["elephant", "tiger", "dragon"];
  var keypress = '';
  var rand = words[Math.floor(Math.random() * words.length)];
  var wordLetters = rand.split('');
  for (var i = 0; i < wordLetters.length; i++) {
    $('.word').append("<span>  " + rand[i] +"  </span>");
    $('.word span').hide();
  }
  $('body').keypress(function(e){
    keypress = (String.fromCharCode( e.which ));
    for (var j in rand) {
      if (rand.hasOwnProperty(j)) {
        if (keypress === rand[j]) {
          //alert('This Alert Works!');
          //sadly this does not work
          $(".word span:nth-child(" + j + ")").show();
        }
      }
    }
  });
});
Answer:1

Because jQuery's implementation of :nth- selectors is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as :eq() or :even jQuery follows JavaScript's "0-indexed" counting. Given a single containing two <li>s, $( "li:nth-child(1)" ) selects the first <li> while $( "li:eq(1)" ) selects the second.

The :nth-child(n) pseudo-class is easily confused with :eq(n), even though the two can result in dramatically different matched elements. With :nth-child(n), all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With :eq(n) only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the (n+1)th one (n is 0-based) is selected.

REF: https://api.jquery.com/nth-child-selector/

Use:

$(".word span").eq(j).show();
//OR
$(".word span:eq(" + j + ")").show();
//One would expect that the following would work since nth- selectors start at 1, but it does not
$(".word span:nth-child(" + (j+1) + ")").show();

Instead of:

$(".word span:nth-child(" + j + ")").show();

$(function(){
  words = ["elephant", "tiger", "dragon"];
  var keypress = '';
  var rand = words[Math.floor(Math.random() * words.length)];
  var wordLetters = rand.split('');
  console.log( rand, wordLetters );
  for (var i = 0; i < wordLetters.length; i++) {
    $('.word').append("<span>  " + rand[i] +"  </span>");
    $('.word span').hide();
  }
  $('body').keypress(function(e){
    keypress = (String.fromCharCode( e.which ));
    for (var j in rand) {
      if (rand.hasOwnProperty(j)) {
        if (keypress === rand[j]) {
          //alert('This Alert Works!');
          //sadly this does not work
          $(".word span:eq(" + j + ")").show();
        }
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="word"></div>
Answer:2

Try ++j, making sure the selector nth-child(0) is not used:

 $(".word span:nth-child(" + ++j + ")").show();
Answer:3

If I use ''.split(',') I get [''] instead of an empty array []. How do I make sure that an empty string always returns me an empty array?

If I use ''.split(',') I get [''] instead of an empty array []. How do I make sure that an empty string always returns me an empty array?

  1. split empty string python
  2. split empty string java
  3. split empty string c#
  4. split empty string javascript
  5. split empty string
  6. split empty string js
  7. split empty string vb.net
  8. split empty string ruby
  9. split empty string perl
  10. split empty string result
  11. split empty string to array
  12. split string empty space c#
  13. split string empty space python
  14. split string empty space java
  15. split string empty values
  16. boost split empty string
  17. split without empty string python
  18. scala split empty string
  19. groovy split empty string
  20. kotlin split empty string

I want to uninstall node-gyp from windows 8.1 through command line is there any way to uninstall it i tried it using command npm uninstall node-gyp from below link I installed node-gyp Link to ...

I want to uninstall node-gyp from windows 8.1 through command line is there any way to uninstall it i tried it using command npm uninstall node-gyp from below link I installed node-gyp Link to ...

  1. uninstall node windows 10
  2. uninstall node windows
  3. uninstall node windows 7
  4. uninstall node-windows service
  5. node uninstall windows command
  6. uninstall node js windows
  7. uninstall node js windows command line
  8. uninstall node-red windows
  9. completely uninstall node windows
  10. uninstall node js windows 10
  11. uninstall node js windows cmd
  12. uninstall node npm windows
  13. uninstall node-gyp windows
  14. uninstall node globally windows
  15. uninstall node version windows
  16. nvm uninstall node windows
  17. fully uninstall node windows
  18. uninstall node modules windows
  19. uninstall node js windows 7
  20. uninstall node completely windows 10

I am using angular's ng-options to populate the html with the names of people. I want it to pre-select the value that I have set as the ng-model (registrantSelected). But for some reason, it won't do ...

I am using angular's ng-options to populate the html with the names of people. I want it to pre-select the value that I have set as the ng-model (registrantSelected). But for some reason, it won't do ...

I'm trying to get some data from a MySQL table for the first time. I'm using ionic , PHP and AngularJS to try it but my query doesn't return anything. As I'm devloping it for my first time, I'm ...

I'm trying to get some data from a MySQL table for the first time. I'm using ionic , PHP and AngularJS to try it but my query doesn't return anything. As I'm devloping it for my first time, I'm ...

  1. mysql data with type
  2. mysql data at rest encryption
  3. mysql data to json
  4. mysql data to excel
  5. mysql.data for visual studio
  6. mysql data to file
  7. mysql.data in c#
  8. mysql data to php table
  9. mysql data to excel export
  10. mysql data in replication
  11. mysql data in where
  12. mysql data and count
  13. mysql data in folder
  14. mysql data to docker
  15. mysql data and structure
  16. mysql data types with examples
  17. mysql data type with decimal
  18. mysql data types with size
  19. get mysql data with javascript
  20. get mysql data with php