javascript - Get each element's attribute through $.each() -
trying $('div').attr('data', 'ip')
through .each()
returns undefined
.
let's have 4 divs
<div class="box" data-ip="ipvalue">content</div> <div class="box" data-ip="ipvalue">content</div> <div class="box" data-ip="ipvalue">content</div> <div class="box" data-ip="ipvalue">content</div>
and need iterate each 1 , data-ip value.
my code, returns undefined (server, serverobj, variables).
var serversobj = $('.server'); serversobj.each(function(index, el) { return $(this).data('ip'); });
what doing wrong?
here code should markup sample gave.
var serversobj = $('.box'), ipvals = []; serversobj.each(function(index, el) { ipvals.push( $(this).data('ip')); }); console.log( ipvals);
you can use $.map() return same array.
Comments
Post a Comment