PDA

View Full Version : Resolved jQuery - Clicks



cancer10
01-28-2009, 05:24 PM
Hi

I have 3 different images on my page, with same IDs for each. And names for each images as img1, img2, img3 respectively

I am using the following function in jQuery, but for some reason, the alert prompts me with the first image name no matter which image i click.




$(document).ready(function(){
$("#img_skin").click(function(){


alert($("#img_skin").attr("name"));
});


Plz help

Thanx

Snookerman
01-28-2009, 05:40 PM
You need to use this:

$(document).ready(function(){

$("#img_skin").click(function(){
alert($(this).attr("name"));

});
Also, an id value should only be used for one element per page. In your case you should use class:

$(document).ready(function(){

$(".img_skin").click(function(){
alert($(this).attr("name"));

});
Of course don't forget to change the ids to classes for your images.

Good luck!

cancer10
01-28-2009, 05:54 PM
Solved.

Thanx for your help.

Snookerman
01-28-2009, 08:14 PM
You're welcome, glad to help! You can go to your first post in this thread, click http://www.dynamicdrive.com/forums/images/buttons/edit.gif then click Go Advanced and add the Resolved prefix to the thread title. This will let other users know the problem has been solved.

Good luck with your site!

codeexploiter
01-29-2009, 03:48 AM
I have 3 different images on my page, with same IDs for each.

This does not seems to be correct idea to have multiple items with the same ID. Technically speaking it breaks the idea of Unique ID.