Results 1 to 5 of 5

Thread: jQuery - Clicks

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question jQuery - Clicks

    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.


    Code:
    $(document).ready(function(){
    				   	$("#img_skin").click(function(){
    												  
    							
    																							alert($("#img_skin").attr("name"));
    																						 });
    Plz help

    Thanx
    Last edited by cancer10; 01-29-2009 at 04:42 AM.

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    You need to use this:
    Code:
    $(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:
    Code:
    $(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!
    Eddy Proca
    I love Dropbox. Get it through my girlfriend's referral link (I reached my limit) and both you and her get 500 MB extra! Thanks and you're welcome!

  3. #3
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    Solved.

    Thanx for your help.

  4. #4
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    You're welcome, glad to help! You can go to your first post in this thread, click 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!
    Eddy Proca
    I love Dropbox. Get it through my girlfriend's referral link (I reached my limit) and both you and her get 500 MB extra! Thanks and you're welcome!

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •