Reputation: 89
how can i retrieve find out which button click is from which div. Basically i have multiple div
<div id="rare1">
<input type=button value="OK" id=btn>
</div>
<div id="rare2">
<input type=button value="OK" id=btn>
</div>
<div id="rare3">
<input type=button value="OK" id=btn>
</div>
i have a function
$("#btn").click(function(){
$("#rare"+i+" #btn").attr("disabled", "true");
}
this way i can disable the latest button being added. how can i select which ever button is click on different div id and select the correct btn to disable?
so sorry forget to add something i want to retrieve the div id that clicked the button too.
Upvotes: 1
Views: 214
Reputation: 382696
An id
must be uniqe per element per page. Currently you have btn
for each button, use a class instead eg class="btn"
and then modify your code like this:
<div id="rare1">
<input type=button value="OK" class=btn>
</div>
<div id="rare2">
<input type=button value="OK" class=btn>
</div>
<div id="rare3">
<input type=button value="OK" class=btn>
</div>
JS:
$(".btn").click(function(){
$(this).prop("disabled", true);
}
To get current clicked button, use $(this)
as shown in code above.
To get parent div id, use:
$(this).parent().attr('id')
So:
$(".btn").click(function(){
var parentId = $(this).parent().attr('id');
$(this).prop("disabled", true);
}
Upvotes: 3
Reputation: 7954
Id is for unique buttons give it class
<div id="rare1">
<input type=button value="OK" class="btn">
</div>
<div id="rare2">
<input type=button value="OK" class="btn">
</div>
<div id="rare3">
<input type=button value="OK" class="btn">
</div>
$(".btn").click(function(){
$(this).attr("disabled", "true");
});
Upvotes: 2
Reputation: 410
You can select the clicked button by using the 'this' var.
$("#btn").click(function(){
$(this).attr("disabled", "true");
}
Upvotes: 1