How to change bootstrap 3 button text using jQuery?

How to get button text inside a tag?

HTML example:

<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 of DISLIKES to YOU DISCLAIMER . My jQuery should change the inner text of the button, not the value of "notThisValue".

+6
source share
2 answers

The reason you cannot change the text is to change the value with .val() . Instead, you need to use .text() to change the text of the tag.

Try it.

 $(document).ready(function(){ $('#like').click(function() { $("#like").text('YOU DISLIKE IT'); }) }); 
+9
source

Use .text() or .html() :

 $(document).ready(function() { $('#dislikes').click(function() { $(this).text('Hello World'); }) }); 
+3
source

Source: https://habr.com/ru/post/970143/


All Articles