I am having a problem of disabling single button
When I click to order for $10 it disables all buttons, and I only want the one which I have clicked to be disabled
JavaScript
x
6
1
$(document).ready(function() {
2
$('.btn-checkout').click(function(e) {
3
$('.btn-checkout').html('proccesing order');
4
$('.btn-checkout').attr("disabled", true);
5
});
6
});
JavaScript
1
7
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<form method="POST">
4
<button type='button' class='btn-checkout'> order for $10</button>
5
<button type='button' class='btn-checkout'> order for $20</button>
6
<button type='button' class='btn-checkout'> order for $30</button>
7
</form>
Advertisement
Answer
Use $(this)
which is the actual clicked item accessed by the selector
JavaScript
1
6
1
$(function() {
2
$('.btn-checkout').on('click',function(e) {
3
$(this).html('proccesing order');
4
$(this).attr("disabled", true);
5
});
6
});
JavaScript
1
7
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<form method="POST">
4
<button type='button' class='btn-checkout'> order for $10</button>
5
<button type='button' class='btn-checkout'> order for $20</button>
6
<button type='button' class='btn-checkout'> order for $30</button>
7
</form>
I would also delegate so you could add more buttons later
JavaScript
1
6
1
$(function() {
2
$('#container').on('click','.btn-checkout',function(e) {
3
$(this).html('proccesing order');
4
$(this).attr("disabled", true);
5
});
6
});
JavaScript
1
9
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<form method="POST">
4
<div id="container">
5
<button type='button' class='btn-checkout'> order for $10</button>
6
<button type='button' class='btn-checkout'> order for $20</button>
7
<button type='button' class='btn-checkout'> order for $30</button>
8
</div>
9
</form>