Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get attr('id') from a <td> using jQuery

Tags:

jquery

I have the following code, and the checkb starts from checkb01 to chekb342.

<tr>
     <td class="specs_under">Specs</td>
     <td id="checkb77" class='checkb'><input type="checkbox" id="add_remove77" class="change_image77"/></td>
     <td  class="specs_under_value" id="specs_unit77">1</td>
     <td rowspan="13" class="specs_under_value" id="specs_itempr77">15</td>
     <td class="specs_under_value" id="specs_quantity77">&nbsp;</td>
     <td class="specs_under_value" id="specs_packageprice77">0.125</td>
</tr>

I tried using this code

$(document).ready(function(){
    $(".checkb").toggle(function() {
        var cid = $(this).attr('id');
        alert('id');

    },function() {
        alert('checkbox unchekced');

    }

How do I get the value of the id in <td> respective checkbox clicks using jquery

Thanks Jean

Guys I need to get the value of id when the checkbox is clicked, not the checkbox id

like image 322
X10nD Avatar asked Oct 09 '10 08:10

X10nD


2 Answers

$(':checkbox').toggle(function() {
    var cid = $(this).attr('id');
    alert(cid);
}, function() {
    alert('checkbox unchekced');
}​);​
like image 82
Darin Dimitrov Avatar answered Nov 05 '22 06:11

Darin Dimitrov


If you are trying to get the id of the enclosing td (not the checkbox itself) consider using the .closest() function.

ex:

$(':checkbox').change(function(){
    var id = $(this).closest('td').attr('id');
    alert(id + ' : ' + this.checked);
});
like image 29
Dan Manastireanu Avatar answered Nov 05 '22 05:11

Dan Manastireanu