SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery each issues - causing multiple instances

    In my page I have multiple instances of the .close class on various objects.

    I am trying to use the jQuery each function to look for all of them, then depending on which is actually clicked, calling an alert.

    Here is a snippet of my code:

    Code JavaScript:
    $(".close").each(function(){
     
    	var removetab = $(this).parent().parent().attr("id");
    	var tabid = removetab.split("-")[1];
     
    	$("#tab-" + tabid + " .close").click(function() {
    		alert($(this).siblings().text());
    	});
     
    });

    This issue is, if there are 4 instances of the .close class on the page, when the first instance is clicked, the alert fires 4 times, the second instance, it fires 3 times, and so on.

    How can I have this just pull each of the .close functions and look for a click on the independent section?

    Thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's bubbling up so you need to stop that.


    Code JavaScript:
        $("#tab-" + tabid + " .close").click(function(event) {
            alert($(this).siblings().text());
            event.stopPropagation();
        });
    Follow me @YaderHernandez


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
  •