Wondering if you can help me out. I seem to have a problem changing the text of my jQuery Mobile buttons with jQuery.
$("#myButton .ui-btn-text").text("New text");
Code above which was recommended for a related question doesn't seem to work.
Neither does:
$("#myButton").attr(value,"New Test");
The code for my button is as followed:
<input type="button" name="answer" id="myButton" data-theme="b" data-answer="4" value="next"></button>
I'll appreciate any feedback guys. Thanks
Update 2
I was working on a fiddle for another question and I noticed that the markup with jQuery Mobile 1.0b2 is a little different:
With this markup, you'd need to do the following to change the text for the button:
Update
Credit where credit is due - As it turns out, @naugtur's answer to this question, which made exactly the same point as my edit, was posted before I got around to correcting my original answer.
I'm not very familiar with jQuery Mobile and the fact that you were using an
<input>
for the button didn't register when I initially answered. Here's my updated answer with a working example showing how you might do it.In the case of an
<input type="button" />
, jQuery Mobile seems to hide the<input>
element and creates a new<a>
element that is styled to look like the button. This is why when you try to get and set the text by using the id of the<input>
as the selector, it doesn't work since that<span>
doesn't have the text that you see on screen. The generated markup looks something like thisI haven't tried out enough examples to be completely confident, but this should work
Here's a working example showing how to change text for both types of buttons (
<a>
and<input type="button">
).I wouldn't recommend using this over
<a>
buttons if you can help it though since there isn't anid
and my approach, at least, relies on the fact that the<a>
corresponding to the<input type="button" />
appears just before the<input>
element.Since jQuery mobile generates HTML around your input type="button" element, and the text that you're seeing isn't actually value of the button, but a text inside of a generated span. You can either traverse the DOM looking for actual span, OR tell jQuery to re-generate button HTML by calling .button("refresh"), like so:
The latter is recommended, since it will stay compatible with future versions of jQuery mobile, while traversing the DOM might break if jQuery team chooses to change structure of the HTML generated for the button.
[deprecated]
Using button inputs has this drawback of not being able to change the attributes of an input in a way that would propagate to jquerymobile's button. Then there is only ne way:
$('#myButton').parent().find('.ui-btn-text').text('zzzzz');
If the
input
button is not necessary to use the application without any javascript present (or you don't want it to work without javascript) then you should always use<a>
tags, because they can be containers.This works for me:
solution from: http://forum.jquery.com/topic/changing-text-works-except-for-buttons
this is very easy, there is a method for this, button('refresh')
good luck!
With jquery.mobile-1.1.0 I was experiencing something slightly different and had trouble sifting through the responses to find a workable answer. I've documented the problems I experienced and the solution I found below.
Snippet 1
With this, I'm able to change the text, but the method clears out the added spans and classes, thus compressing the button.
HTML
Snippet 2
This had no effect upon the button text.
HTML
Snippet 3
This generated the following error message:
cannot call methods on button prior to initialization; attempted to call method 'refresh'
Which was confusing to me because this function is being called only after the page is initialized. Reading further, I then noticed Priyank's reference to a working answer at stackoverflow.com/a/7279843/61821
Snippet 4 - Working
A better jQuery selector works like a charm.