Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Change Value of Clicked button with $(this) selector

I am trying to update the text of a button when it is clicked. I have a number of buttons with the same class but no id. Why doesn't this work?

<input type="button" class="add-button" value="Default" />
<input type="button" class="add-button" value="Default" />
<input type="button" class="add-button" value="Default" />
<input type="button" class="add-button" value="Default" />

$(document).ready(function() {
    $('.add-button').click(function() {
        alert('clicked');
        $(this).html('Changed');
    });
});​

The alert is triggered, but the button isn't changed.

like image 932
shuniar Avatar asked Dec 07 '25 08:12

shuniar


1 Answers

Use this.value for input. See below,

$(document).ready(function() {
    $('.add-button').click(function() {
        alert('clicked');
        this.value = 'Changed';
    });
});​

DEMO: http://jsfiddle.net/qU5tR/10/

like image 142
Selvakumar Arumugam Avatar answered Dec 08 '25 22:12

Selvakumar Arumugam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!