Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery loop through data() object

Tags:

Is it possible to loop through a data() object?

Suppose this is my code:

$('#mydiv').data('bar','lorem');   $('#mydiv').data('foo','ipsum');   $('#mydiv').data('cam','dolores'); 

How do I loop through this? Can each() be used for this?

like image 910
bart Avatar asked Apr 21 '09 13:04

bart


People also ask

How do I iterate data in jQuery?

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.

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.

What is the use of each () function in jQuery?

each() jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It's very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.

How do I traverse an array in jQuery?

Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.


1 Answers

$.each($.data(this), function(i, e) {    alert('name='+ i + ' value=' +e); }); 

This will iterate through each property in 'this' element's data object.

like image 139
John Strickler Avatar answered Sep 26 '22 06:09

John Strickler