I want to send the value of clicked button using ajax.
here's the buttons code:
<button id="accept" value="1">Accept</button>
<button id="reject" value="1">Reject</button>
<br><button id="accept" value="2">Accept</button>
<button id="reject" value="2">Reject</button>
<br><button id="accept" value="3">Accept</button>
<button id="reject" value="3">Reject</button>
and the jquery code:
<script>
var url = 'server.php';
$("#accept").click(function() {
$.ajax({
url : url,
type: 'post',
data : {
id : $("#accept").val(),
action : 'accept',
}
});
});
$("#reject").click(function() {
$.ajax({
url : url,
type: 'post',
data : {
id : $("#reject").val(),
action : 'reject'
}
});
});
</script>
The 'id' sent in ajax should be the value of the pressed button.
Your help would be greatly appreciated.
Please don't use duplicate id. id
should be unique.
And As a button value is an attribute you need to use the .attr() method in jquery. This should do it
<script type="text/javascript">
$(document).ready(function() {
$('.my_button').click(function() {
alert($(this).attr("value"));
});
});
</script>