evan evan - 3 months ago 14
HTML Question

Add data-action to all img that contain a class

I'm using this plugin: https://github.com/fat/zoom.js/

I'd like to select all

img
that have the class
img-responsive
and add
data-action="zoom"
to the element. Is this possible using jQuery? Is the performance going to be really poor?

Answer

Use jQuery.data( key, value ) method

$('img.img-responsive').data('action','zoom');

Or using jQuery.attr( attributeName, value ),

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