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>
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.
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.
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.
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>
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With