Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace jQueryUI button text?

I've got a button that I use with jQueryUI somethink like this (simplified).

<button id="mybutton">Play<button> <script> $("#mybutton").button().toggle(     function(){        $(this).text('Stop');     },     function(){        $(this).text('Start');     }, ); </script> 

This code breaks the way the button looks because when making it into the button widget, there's a new span added inside the button. So I'm changing the button value like this now

$(this).find('span').text('Stop'); 

This is hacky because I can't treat the button as a black box anymore and have to go inside.

Is there a clean way to do this?

like image 985
Andrei R Avatar asked Apr 19 '10 07:04

Andrei R


People also ask

How do I change the button text?

To change the button text, first we need to access the button element inside the JavaScript by using the document. getElementById() method and add a click event handler to the button, then set it's value property to blue . Now, when we click on our button , it changes the value from Red to Blue or vice versa.


2 Answers

Maybe you could use the label option of the jQuery UI button now instead?

$("#mybutton").button().toggle(function() {    $(this).button('option', 'label', 'Stop'); }, function() {    $(this).button('option', 'label', 'Start'); }); 

jsbin preview here

like image 77
gnarf Avatar answered Sep 30 '22 18:09

gnarf


You can use the .button("refresh") method after you alter the text.

 $("#mybutton").button().toggle(     function(){        $(this).text('Stop').button("refresh");     },     function(){        $(this).text('Start').button("refresh");     }, ); 

http://jqueryui.com/demos/button/#method-refresh

like image 34
ajpiano Avatar answered Sep 30 '22 19:09

ajpiano