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();
?
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.
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');
$('.xyz', parentElement);
end()
, to get#mySelect
closest
.(find)
so it's more efficient. stackoverflow.com/questions/12674591/…