phil294
phil294

Reputation: 10872

Difference between `space` and find() in selector

I want to remove all options but the first from my <select/>. I understand that children() would not work recursively.

Is there any difference between

$('#mySelect :gt(0)').remove();

and

$('#mySelect').find(':gt(0)').remove();

?

Upvotes: 0

Views: 267

Answers (2)

Rama Rao M
Rama Rao M

Reputation: 3051

find() method is useful if we already have parent element reference:

 var parentElement = $('#mySelect');
    /*
    * there is some code..
    * do some thing on parent
    */

Now if we want to get parentElement's children we can use either

parentElement.find('.xtz'); or $('.xtz',parentElement)

rather than using complete selector either

$('#mySelect .xtz'); or $('#mySelect').find('.xtz');

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074959

Shockingly I can't find a duplicate of this question, all the others are about speed.

There's no significant difference in the two lines of code you've quoted, no.

Upvotes: 3

Related Questions