views:

170

answers:

3

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?

A: 

Is the id of the button always myButton? If yes, then just use

$("#myButton").text('Stop');

psychotik
doesn't work. that's what i'm using.
Andrei Railean
how's that span added? If inside `myButton` then it won't work, obviously. You can do this in that case: $("#myButton span").text('Stop'); which is equivalent to your code. Why is that span _inside_ the button tag though? That sounds wrong...
psychotik
yeah. that's kind of what i'm doing, but i don't want to do it that way.
Andrei Railean
umm... whats the origin of that span then? I don't see how you can do this any other way without getting rid of the span.
psychotik
deleted --- added to wrong place
Tim
deleted ...................
Tim
+1  A: 

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

ajpiano
thanks, but this doesn't work either
Andrei Railean
i even tried pretending that i'm creating the button from scratch and it still doesn't work $(this).text('Start'); $(this).button();Breaks the button as well
Andrei Railean
+4  A: 

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

gnarf
Thank you. This does the trick.
Andrei Railean