Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text of a Bootstrap 3 button with jQuery?

How to retrieve button text inside tag button?

Example HTML:

<button type="button" class="btn btn-danger" id="dislikes" value="notThisValue">DISLIKES<span class="glyphicon glyphicon-thumbs-down"></span></button>

My jQuery:

$(document).ready(function(){
    $('#dislikes').click(function() {
        $("#dislikes").val('Hello World');
    })
});

I need to change the text DISLIKES to YOU DISLIKE IT. My jQuery has to change the inner text of the button not the value "notThisValue".

like image 628
Ignatius Samuel Megis Avatar asked Jun 01 '14 10:06

Ignatius Samuel Megis


2 Answers

The reason you are not able to change the text is that you are changing its value using .val(). Instead you need to use .text() to change tag's text.

Try this.

$(document).ready(function(){
$('#like').click(function() {
 $("#like").text('YOU DISLIKE IT');
})
});
like image 97
Hammad Avatar answered Oct 15 '22 18:10

Hammad


Use .text() or .html():

$(document).ready(function() {
    $('#dislikes').click(function() {
        $(this).text('Hello World');
    })
});
like image 39
rink.attendant.6 Avatar answered Oct 15 '22 18:10

rink.attendant.6