I need to get the div containing the street address within the list. The div has a class called address ( div class="address" )
I cannot use jQuery("#storeList li .address"), because there are other elements I need to acces as well.
I have the following code:
jQuery("#storeList li").each(function() {
var n = jQuery(this.address).text(); // <- This does not work
alert(n);
});
How do I access each DIV element of type Address?
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
The each() method in jQuery specifies a function that runs for every matched element. It is one of the widely used traversing methods in JQuery. Using this method, we can iterate over the DOM elements of the jQuery object and can execute a function for every matched element.
each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.
Answer: Use the jQuery each() Method You can simply use the jQuery each() method to loop through elements with the same class and perform some action based on the specific condition.
jQuery("#storeList li").each(function() {
var n = jQuery(this).find(".address").text(); // <- This works
alert(n);
});
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