Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
like image 560
ryandlf Avatar asked Mar 22 '12 02:03

ryandlf


People also ask

How do I get all HTML elements in jQuery?

Get Content - text(), html(), and val() Three simple, but useful, jQuery methods for DOM manipulation are: text() - Sets or returns the text content of selected elements. html() - Sets or returns the content of selected elements (including HTML markup) val() - Sets or returns the value of form fields.

How do I iterate through a div in jQuery?

jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children() method is called to find all the child elements of the selected element.

How do I select all elements in a div?

getElementsByTagName() that will select all the instances of a certain HTML element on the current webpage based on its tag name, i.e. <div> . Calling document. getElementsByTagName("div") is all you need to do to select all <div> elements on the current page using JavaScript.


1 Answers

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>
    });
});​
like image 141
Terry Avatar answered Nov 13 '22 04:11

Terry