Yang
Yang

Reputation: 6912

JQM: programmably change the label for an input

I have a text input with a label:

<li data-role="fieldcontain">
<label for="number">Amount:</label>
<input type="number" name="number" id="vitalamount" value=""  />
</li>

I'm now trying to change the label of the input, but the following code didn't work.

$("#vitalamount").attr("label","something else");

Upvotes: 1

Views: 127

Answers (2)

Jashwant
Jashwant

Reputation: 29025

If you don't include the form field inside the label tag, then you should use the for attribute to associate it with the id of the form element you want to label.

Reference

So, put id in for attribute not name.

Amount:

Now, if you the id of input as vitalamount

$('label[for="vitalamount"]').html('New value of label');

Upvotes: 0

Ram
Ram

Reputation: 144739

attr modifies the attributes of an element not it's sibling elements, you can use prev() or siblings for selecting the label and changing it's texts:

$("#vitalamount").siblings("label").text('something else');

Upvotes: 3

Related Questions