jqueryjquery-selectorstree-traversal

jquery :not selector not working in next() method


what is the next best thing to use when you want to select the next li item, but not the one that has someClassName. The not selector returns an empty array!

or is this a case off using filter?

 <ul class="items">
    <li class="first">pickle</li>
    <li class="someClassName">tomato</li>
    <li>chicken</li>
    <li>cocosnut</li>
  </ul>

var current = $('ul.items li.first');
var next =  current.next(':not(li.someClassName)');

thanks, Richard


Solution

  • next only selects the next sibling of the selected element. You can use nextAll method:

    var next = current.nextAll(':not(.someClassName)').first();
    

    http://jsfiddle.net/P3EpK/

    Note that your markup is not valid.