1

I'm trying to take data from another part of the element and add it to another (force lazy-loading images to load) and everything I find points me towards setAttribute however this breaks the data as it automatically adds a equals and quotations to the added data which breaks it.

So:

images[i].setAttribute('data-anim-src',' srcset= "'+images[i].getAttribute('data-srcset')+'"');

Gets me data-anim-src=" srcset="..."" instead of what I want which is data-anim-src srcset="..."

Is there any way to remove the ="" that's automatically added with setAttribute? or is there an equally simple alternative?

1 Answer 1

3

setAttribute works like this:

setAttribute(attribute, value);

not like this:

setAttribute(attribute1, attribute2, attribute3);

So to solve it, just set them separately:

images[i].setAttribute('data-anim-src', 'true');
images[i].setAttribute('srcset', images[i].getAttribute('data-srcset'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.