613

How do you change the text value of a button in jQuery? Currently, my button has 'Add' as its text value, and upon being clicked I want it to change to 'Save'. I have tried this method below, but so far without success:

$("#btnAddProfile").attr('value', 'Save');
Flip
  • 6,233
  • 7
  • 46
  • 75
user517406
  • 13,623
  • 29
  • 80
  • 120
  • 3
    actually your example should work to change the value of the button. I tried it and it worked. Maybe the id of the button is different or a typo. – mjspier Apr 07 '11 at 12:05
  • Still doesn't work...could JQuery UI styles be causing this? – user517406 Apr 07 '11 at 12:50
  • 4
    Sergey's answer was best. it is working properly on jquery buttons without removing its styles, padding and margins. – AaA Mar 07 '13 at 02:39

20 Answers20

1032

Depends on what type of button you are using

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").attr('value', 'Save'); //versions older than 1.6

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").prop('value', 'Save'); //versions newer than 1.6

<!-- Different button types-->

<button id='btnAddProfile' type='button'>Add</button>
$("#btnAddProfile").html('Save');

Your button could also be a link. You'll need to post some HTML for a more specific answer.

EDIT : These will work assuming you've wrapped it in a .click() call, of course

EDIT 2 : Newer jQuery versions (from > 1.6) use .prop rather than .attr

EDIT 3 : If you're using jQuery UI, you need to use DaveUK's method (below) of adjusting the text property

Community
  • 1
  • 1
JohnP
  • 49,507
  • 13
  • 108
  • 140
  • 8
    Got it working using .html('Save'), I didn't notice that I had set runat=server on my button, this is what was causing the issue. – user517406 Apr 07 '11 at 12:59
  • 8
    I would downvote this if I could, as it messes up the style in some cases. *Please use the answer of DaveUK, if you have trouble with this one*. PS: I guess that's what Sevenearths noticed, too – user562529 Apr 10 '12 at 19:24
  • 3
    Even better: use the answer from Sergey below for proper use of jQuery and future-proofing: $( ".selector" ).button( "option", "label", "new text" ); – cincodenada Sep 01 '12 at 01:07
  • For `button` elements, you can also use `$("#element").val("New Label");` – Alex Apr 01 '21 at 20:09
161

For buttons created with .Button() in jQuery........

Whilst the other answers will change the text they will mess up the styling of the button, it turns out that when a jQuery button is rendered the text of the button is nested within a span e.g.

<button id="thebutton">
  <span class="ui-button-text">My Text</span>
</button>

If you remove the span and replace it with text (as in the other examples) - you'll loose the span and associated formatting.

So you actually need to change the text within the SPAN tag and NOT the BUTTON!

$("#thebutton span").text("My NEW Text");

or (if like me it's being done on a click event)

$("span", this).text("My NEW Text");
DaveUK
  • 1,743
  • 1
  • 10
  • 4
  • 4
    You shouldn't be expecting this DOM structure to never change. Far better is to use the method jQuery-UI gives you to change the label (see Sergey's answer). – Mike DeSimone Sep 12 '13 at 14:36
84

The correct way of changing the button text if it was "buttonized" using JQuery is to use .button() method:

$( ".selector" ).button( "option", "label", "new text" );
Perception
  • 79,279
  • 19
  • 185
  • 195
Sergey
  • 2,303
  • 1
  • 18
  • 11
  • 3
    This worked for me better than the other approaches, which stomped on the styling of the button (in particular the size of the button). I was using a button on a jQuery UI dialog, FWIW. – Dave Crumbacher Aug 20 '12 at 20:13
  • 8
    This is the correct answer for buttons created with jQuery, such as those on a dialog. All the others will destroy some of the jQuery styling. This also does it without depending on the HTML structure that jQuery generates, which is more future-proof. – cincodenada Sep 01 '12 at 01:07
  • This is the right answer to this question (it seems the question is about Jquery UI buttons, see comments), it should be promoted. – peveuve Jul 02 '15 at 15:25
41

To change the text in of a button simply execute the following line of jQuery for

<input type='button' value='XYZ' id='btnAddProfile'>

use

$("#btnAddProfile").val('Save');

while for

<button id='btnAddProfile'>XYZ</button>

use this

$("#btnAddProfile").html('Save');

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
  • Also, for buttons created by jquery using e.g. `$('#thing').append($("")....)`, you need to use .html to set the text. – Benubird Feb 22 '13 at 10:27
32

Use .val()

Here's a link to JSfiddle

23

You need to do .button("refresh")

HTML :

<button id='btnviewdetails' type='button'>SET</button>

JS :

$('#btnviewdetails').text('Save').button("refresh");
Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
user1464277
  • 231
  • 3
  • 6
  • For some reason, it would only work for me after I used your code to refresh the button, but I also noticed that the icon on the button (part of JQuery Mobile CSS) gets lost after refreshing it (although your solution was by the closest I got). – Ciaran Gallagher Mar 27 '13 at 00:59
23

You can use something like this:

$('#your-button').text('New Value');

You can also add extra properties like this:

$(this).text('New Value').attr('disabled', true).addClass('bt-hud').unbind('click');
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
PurplePier
  • 339
  • 2
  • 4
14

$("#btnAddProfile").text("Save");

i_emmanuel
  • 519
  • 6
  • 6
12

If you have button'ed your button this seems to work:

<button id="button">First caption</button>

$('#button').button();//make it nice
var text="new caption";
$('#button').children().first().html(text);
Gluip
  • 2,917
  • 4
  • 37
  • 46
9

You can use

$("#btnAddProfile").text('Save');

although

$("#btnAddProfile").html('Save');

would work as well, but it's misleading (it gives the impression you could write something like

$("#btnAddProfile").html('Save <b>now</b>');

but of course you can't

gotofritz
  • 3,341
  • 1
  • 31
  • 47
6

it's work for me html:

<button type="button" class="btn btn-primary" id="btnSaveSchedule">abc</button>

js

$("#btnSaveSchedule").text("new value");
Wolf
  • 6,361
  • 2
  • 28
  • 25
5
$("#btnAddProfile").click(function(){
    $("#btnAddProfile").attr('value', 'Save');
 });
sauerburger
  • 4,569
  • 4
  • 31
  • 42
4

$(document).ready(function(){
    $(":button").click(function(){
        $("p").toggle();

    if (this.value=="Add") this.value = "Save";
    else this.value = "Add";

  });
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>

<input type='button' value='Add' id='btnAddProfile'>

<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>

</body>
</html>
Nandhi Kumar
  • 343
  • 1
  • 11
4
document.getElementById('btnAddProfile').value='Save';
curtisk
  • 19,950
  • 4
  • 55
  • 71
2

Have you gave your button a class instead of an id? try the following code

$(".btnSave").attr('value', 'Save');
Raj
  • 22,346
  • 14
  • 99
  • 142
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
0

This should do the trick:

$("#btnAddProfile").prop('value', 'Save');       
$("#btnAddProfile").button('refresh');
0
$("#btnAddProfile").html('Save').button('refresh');
Drastick
  • 317
  • 5
  • 13
0
$("#btnviewdetails").click(function(){
    if($(this).val()!="hide details"){
        $("#detailedoutput").show();
        $(this).val('hide details');
    }else{
        $("#detailedoutput").hide();
        $(this).val('view more details');
    }

});
Fengzmg
  • 710
  • 8
  • 9
-1
    $( "#btnAddProfile" ).on( "click",function(event){
    $( event.target ).html( "Save" );
});
VladL
  • 12,769
  • 10
  • 63
  • 83
-1

that's exactly correct, this works for me;

    $('#btnAddProfile').bind('click',function(){
    $(this).attr('value','save');
})

are you sure Jquery is available and that your code is in the correct place?

John Beynon
  • 37,398
  • 8
  • 88
  • 97