user3371049 user3371049 - 5 months ago 8
Javascript Question

How to change the name "data-original" attribute name in images with no ids

Context: I am trying a workaround to the lazyload script that does not work well for my setup.

I am not sure this is realistic but, this is what I have in mind:
On a title section click, I want to change all the attributes of the images contained in the section (so the images show).

From data-original to src.

Here is were I am:

$('#s101').click(function(){

var a = $('#b01').next().find('img').attr("data-original");

// alert (a) // will give me the url of the attr. data-original: (http...)

x = a.getAttribute("data-original");
a.setAttribute("src", x);
a.removeAttribute("data-original");
});


This does not work. Could you help?

Answer

Try this

$('#b01').next().find('img').each(function(){
   $(this).attr("src", $(this).attr("data-original") ).removeAttr("data-original");
});
Comments