0

I have the following images:-

<img rel="item-1" src="1.gif"/>
<img rel="item-2" src="2.gif"/>
<img rel="item-3" src="3.gif"/>

and at the moment I can use Jquery to attach a click event to each image like so:-

$("img[rel^='item']").click(function () {
    var img = $(this);
});

Suppose I want to use the data attribute and remove the rel attribute:-

<img data-thumb="item-1" src="1.gif"/>
<img data-thumb="item-2" src="2.gif"/>
<img data-thumb="item-3" src="3.gif"/>

What would my jquery look like to attach a click event to each image that has the data-thumb attribute?

EDIT Sorry guys it is the same, like an idiot I forgot to change this in my code:-

var url = img.attr('rel'); to var url = img.data('thumb');

I feel ashamed!

4 Answers 4

2

It's the same :)

$("img[data-thumb^='item']").click(function () {
    var img = $(this);
});
Sign up to request clarification or add additional context in comments.

Comments

1

You replace rel in your selector with data-thumb:

$("img[rel^='item']").click(function () {
    var img = $(this);
});

becomes

$("img[data-thumb^='item']").click(function () {
    var img = $(this);
});

Comments

1

Simply :

$("img[data-thumb^='item']").click(function () {
    var img = $(this);
});

Comments

1

Doesnt the same work?

$("img[data-thumb^='item']").click(function () {
    var img = $(this);
});

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.