In my html template I've got a label with an input radio inside:
<label id="myid" class="inline">
<input type="radio" value ="11">
my text
</label>
I need to change the text of the label with jquery. I tried this:
$('#myid').html('my new text');
or
$('#myid').text('my new text');
but when I do that I lose my input radio. How can I preserve the input radio inside the label and modify label's text?
In my html template I've got a label with an input radio inside:
<label id="myid" class="inline">
<input type="radio" value ="11">
my text
</label>
I need to change the text of the label with jquery. I tried this:
$('#myid').html('my new text');
or
$('#myid').text('my new text');
but when I do that I lose my input radio. How can I preserve the input radio inside the label and modify label's text?
Share Improve this question edited Sep 16, 2019 at 10:02 kiks73 asked Aug 18, 2014 at 16:01 kiks73kiks73 3,7583 gold badges28 silver badges55 bronze badges 1-
$('#myid input').get(0).nextSibling.nodeValue = 'my new text';
– adeneo Commented Aug 18, 2014 at 16:15
2 Answers
Reset to default 11Try this:
$('#myid').contents().last().replaceWith('my new text');
Put your text in a span
and try this:
<label id="myid" class="inline">
<input type="radio" value ="11">
<span>my text</span>
</label>
$('#myid').find("span").text('my new text');
Check JSFiddle Demo