Passing data to a jQuery click() function

16,146

Solution 1

If you insist on using old-school HTML 4.01 or XHTML:

$('.removeAction').click(function() {
 // Don’t do anything crazy like `$(this).attr('id')`.
 // You can get the `id` attribute value by simply accessing the property:
 this.id;
 // If you’re prefixing it with 'my' to validate as HTML 4.01, you can get just the ID like this:
 this.id.replace('my', '');
});

By the way, in HTML5, the id attribute can start with a number or even be a number.

Then again, if you’re using HTML5 anyway, you’re probably better off using custom data attributes, like so:

<span class="action removeAction" data-id="1">Remove</span>

Solution 2

$(this) within your click function represents the clicked element

$(".removeAction").click(function() {
    //AJAX here that needs to know the ID
    alert($(this).attr('id'));           
}

Solution 3

The following code will get you the ID of the clicked span. Using the ID isn't perfect, but it will work.

$(".removeAction").click(function() {
  alert($(this).attr("id"));
});

Solution 4

You could have a hidden field on each row which stores the ID and/or other data needed in a JSON object & use that instead of hacking around with the span tag.

<tr>
<td>
<span class="action removeAction">Remove</span>
<input type="hidden" value="1" />
</td>
</tr>

$('.removeAction').click(function(){
  var id = $(this).next().val();
  // do something...
});

HTH

Share:
16,146

Related videos on Youtube

Jake N
Author by

Jake N

I am Jake. I work on bespoke web applications and web sites. I use PHP, MySQL, jQuery, Knockout JS, Zend Framework, Symfony2, Concrete5 CMS. I live in Derby, UK. I am on LinkedIn

Updated on April 21, 2022

Comments

  • Jake N
    Jake N about 2 years

    I have a simple span like so

    <span class="action removeAction">Remove</span>
    

    This span is within a table, each row has a remove span.

    And then I call a URL using AJAX when that span is clicked. The AJAX event needs to know the ID of the object for that row? What is the best way of getting that ID into the click function?

    I thought I could do something like this

    <span class="action removeAction" id="1">Remove</span>
    

    But an ID should not start with a number? Right? Then I thought I could do

    <span class="action removeAction" id="my1">Remove</span>
    

    Then just strip the 'my' part from the ID, but that just seems Yuk!

    Below is my click event and where my AJAX event is.

    <script type="text/javascript" language="text/javascript">
    
    $(document).ready(function()
    {
    
        $(".removeAction").click(function()
        {
            //AJAX here that needs to know the ID            
        }
    });
    
    </script>
    

    I am sure there is a nice way of doing this?

    Note: I am not looking for

    $(this).attr("id");
    

    I want to be able to pass more than one piece of information

    Thanks. Jake.

  • Jake N
    Jake N almost 14 years
    Thanks Glen. This isn't what I am looking for [will edit my post].
  • Jake N
    Jake N almost 14 years
    Thanks Tim. This isn't what I am looking for [will edit my post].
  • Mathias Bynens
    Mathias Bynens almost 14 years
    Why use $(this).attr('id') instead of this.id?
  • Jake N
    Jake N almost 14 years
    Ah, custom attributes are OK? I thought they were W3C badness?
  • Jake N
    Jake N almost 14 years
    Didn't know that this.id was the same - nice
  • Mathias Bynens
    Mathias Bynens almost 14 years
    @jakenoble: Why would they be? They’re awesome, and very useful in cases like yours: w3.org/TR/html5/dom.html#custom-data-attribute
  • Mathias Bynens
    Mathias Bynens almost 14 years
    @jakenoble: It’s called JavaScript. :)
  • Mathias Bynens
    Mathias Bynens almost 14 years
    Why use $(this).attr('id') instead of this.id?
  • Jake N
    Jake N almost 14 years
    Haha. Head stuck in jQuery and PHP too long to remember
  • Jake N
    Jake N almost 14 years
    Thanks. Not thought of that. Somewhat chunky I feel and not as elegant as custom attributes.
  • Sunny
    Sunny almost 14 years
    @jakenuble - I agree to the "chunky"-ness of the solution. The page might not validate for xHTML with custom attributes...
  • Sunny
    Sunny almost 14 years
    @jakenoble - this is a good place to see some arguments for/against custom attributes: stackoverflow.com/questions/994856/…
  • Flatlin3
    Flatlin3 almost 14 years
    To keep it the jQuery-Way :-) I do this strictly to avoid possible x-browser problems ...