Reputation: 28555
Is it possible to get all div's within a page using jQuery including nested divs?
$('div'); //returns outer but not inner
<div id="outer">
<div id="inner"></div>
</div>
Upvotes: 8
Views: 25479
Reputation: 14219
Make sure the DOM is loaded yo.
$(function() {
console.log($('div'));
// [<div id="outer"><div id="inner"></div></div>], [<div id="inner"></div>]
$('div').each(function(i, ele) {
console.log(i + ': ' + ele);
// 0: <div id="outer"><div id="inner"></div></div>
// 1: <div id="inner"></div>
});
});
Upvotes: 12
Reputation: 102134
// Shorthand for ready
$(function(){
$('div');
// or use the DOM method:
document.getElementsByTagName('div');
});
Upvotes: 4