Reputation: 3939
I create a grid in C# MVC 3. and place a delete button in every rows in the grid.
Please see my image below.
Delete button code.
<td>
<button id="removefromcart" type="button" name="removefromcart"
class="remove-cartitem" value="@(item.Id)"> </button></td>
Script :
<script type="text/javascript">
$(document).ready(function () {
$('[name="removefromcart"]').click(function () {
alert('clicked');
});
})
I try to alert the value of the clicked button.
Please help
Upvotes: 0
Views: 3722
Reputation: 8275
Simply get the value of the DOM element :
$('[name="removefromcart"]').click(function () {
alert(this.value);
});
EDIT :
this.value
is equivalent to $(this).val()
or $(this).attr('value')
but fastest since we only manipulate DOM attribute (and not a jQuery object)...
See for example : jsPerf
Upvotes: 1
Reputation: 184
Your button has an id, i guess it would be better to use this ID to get the element in jquery.
Moreover, the click() function is deprecated, you should use on('click') which allows you to unbind the event with off('click').
$('#removefromcart').on('click', function() {
alert( $(this).val() );
});
The best way would be not to use jQuery
document.getElementById('removefromcart').addEventListener('click', function() {
alert( this.attributes.value.nodeValue );
});
Hope this helps :)
Upvotes: 1
Reputation: 8346
$('[name="removefromcart"]').click(function () {
alert($(this).val());
});
Upvotes: 1
Reputation: 64466
use class
attribute
of dom element
<script type="text/javascript">
$(document).ready(function () {
$('.remove-cartitem').click(function () {
alert($(this).val());
});
});
</script>
Upvotes: 2
Reputation: 12974
Try this:
$('.remove-cartitem').click(function () {
alert($(this).attr('value'));
});
Upvotes: 2