Where is more logical to use .find

.find instead of with an id?

I saw both examples bot not clearly whitch case where.

$('.element').find('span').html('Do something');

$('span').html('Do something');

So both select that what is the logic to choice one?


The second one selects all <span> elements on the page. The first one selects ones that are children of an element with the class .element

1 Like

Thanks and what about


$('.element').find('span').html('Do something');

$('#idforspan').html('Do something');


<div class="element"><span id="idforspan">Do something</span></div>

Like why good to use with .find than other direct id? What the logic if both select same?


No point using find there: you’re selecting one specific element, and there can only be one of those elements on the page. And note for both the examples:

// Collection of elements
document.querySelectorAll('.element span')
// Specific element (first one found)