.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?
Thanks!
.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?
Thanks!
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
Thanks and what about
JS
$('.element').find('span').html('Do something');
$('#idforspan').html('Do something');
HTML
<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?
Thanks!
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('span')
document.querySelectorAll('.element span')
// Specific element (first one found)
document.querySelector('#idforspan')