How to add class to first child with jQuery?

The cleanest way of achieving this would be:

$('#resultsBox li').eq(2).addClass('selected');

Documentation on the .eq method can be found here: http://api.jquery.com/eq/


Use :first selector:

$('#resultsBox li:first').addClass('aaaa');

and for the third element selection, you can use each() method: Here is jsFiddle.

$('ul li').each(function(i) {
    if ( i === 2 ) {
       $(this).addClass('aaaa');
    }
});

or you can do this with eq method like Jamiec & MrThys mentioned: but each method will be much usefull when things getting complicated.

$('#resultsBox li').eq(2).addClass('aaaa');