ryandlf
ryandlf

Reputation: 28555

Get ALL DIV (Or any element) in a Page with jQuery

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

Answers (2)

Terry
Terry

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

max
max

Reputation: 102134

// Shorthand for ready
$(function(){
  $('div');

  // or use the DOM method:
  document.getElementsByTagName('div');
});

Upvotes: 4

Related Questions