Zack
Zack

Reputation: 521

Adding a attribute to all the images in a web page using javascript

I'm using zoom which is a simple jQuery plugin for image zooming, as seen on Medium.

It requires adding a data-action="zoom" to all the <img> tags to work.

Here's my Fiddle.

I want it to work with all the images in a web page by a using a js code that applies the attribute to all <img> tags.

Upvotes: 0

Views: 70

Answers (2)

Leo the lion
Leo the lion

Reputation: 3065

What you can do is use attribute setter .attr(attributeName, value).

$('img').attr("data-action","zoom")

see in action

Upvotes: 3

Dhara Parmar
Dhara Parmar

Reputation: 8101

Add like:

$( "img" ).each(function( index ) {
   $(this).attr("data-action","zoom")
});

Upvotes: 2

Related Questions