Reputation: 531
I'm trying to add data-name
to every parent div of images. I was trying:
<div><img src="..." alt=""/></div>
... 40 times (<div><img src="..." alt=""/></div>)
<div><img src="..." alt=""/></div>
js:
$("img").load(function() {
$(this).parent().attr('data-name', 'abc')
});
On this way, only 1/3 of div was added. 2/3 above not. How can I fix it?
Upvotes: 1
Views: 146
Reputation: 2329
The answer for your second question could be this one:
$(window).on("load", function(){
$('img').each(function(){
var width = $(this).width();
console.log(width)
$(this).parent().attr('data-name', 'abc').attr('data-size', width);
})
})
Note: I m checking here for the image width which I think will be the necessary one, if you need the height just change it.
Upvotes: 1
Reputation: 2329
If you don't have problem to load so many images (some preloading will be better) you can just simply use:
$(window).on("load", function(){
$('img').parent().attr('data-name', 'abc');
})
Upvotes: 1