X10nD
X10nD

Reputation: 22050

Get ID from <td> using jQuery

I have <td id="checkb1" class="checkba">

I want to get the value of the id when I click on

$('.checkba').click(function() {

    var my_var = $('.checkba').attr('id');
    alert(my_var);

});

The alert shows checkba not the id, checkb1.

Appreciate all assistance.

Thanks Jean

Upvotes: 0

Views: 1748

Answers (1)

Nick Craver
Nick Craver

Reputation: 630637

You can use this, to refer to the element you clicked on, like this:

$('.checkba').click(function() {
  alert(this.id);
});

Otherwise .attr() gets the attribute from the first matched element.

Upvotes: 4

Related Questions