8

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?

kiks73
  • 3,718
  • 3
  • 25
  • 52

2 Answers2

11

Try this:

$('#myid').contents().last().replaceWith('my new text');
antyrat
  • 27,479
  • 9
  • 75
  • 76
6

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

Moshtaf
  • 4,833
  • 2
  • 24
  • 34