Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector for a specific li

Tags:

jquery

I have the following markup

<div id="search-query-autocomplete">
  <ul id="autocomplete-results">
    <li class="auto-li">blue</li>
  </ul>
</div>

and want to add a class to the first auto-li when the user keys down.

I have the following javascript:

document.onkeydown = checkKey;
 function checkKey(e){
 e = e || window.event;

 if(e.keyCode=='40')
 {
   $('#autocomplete-results li:nth-child(0)').text('not working');
   console.log('here is 40'); //code
 }
}

But it's not working - what do I need to change?

thx in advance

like image 727
timpone Avatar asked Jan 16 '23 04:01

timpone


1 Answers

The :nth-child pseudo-class starts at index 1 not 0

The nth child pseudo-class won't actually help in selecting the first .auto-li, you can use :first, .eq(0) or :eq(0) to filter the selection to just the first .auto-li

$('#autocomplete-results li.auto-li').eq(0).text('working');
like image 200
Musa Avatar answered Jan 25 '23 18:01

Musa