Skip to content Skip to sidebar Skip to footer

Jquery Find To Get The First Element

I am writing $(this).closest('.comment').find('form').toggle('slow'); and the problem is each of the forms in the child is being toggled. I would like only the first form to be to

Solution 1:

You can use either

$(this).closest('.comment').find('form').eq(0).toggle('slow');

or

$(this).closest('.comment').find('form:first').toggle('slow');

Solution 2:

Use :first selector like below :

$(this).closest('.comment').find('form:first').toggle('slow');

Solution 3:

using jquery simply use:

    $( "form" ).first().toggle('slow');

Solution 4:

I use

$([selector]).slice(0, 1)

because it's the most explicit way to select a slice of a query and because it can be easily modified to match not the first element but the next, etc.

Solution 5:

The simplest way to get the first result of find is with good old [index] operator:

$('.comment').find('form')[0];

Works like a charm!

Post a Comment for "Jquery Find To Get The First Element"