How can I change a label text without interfering with an input radio inside the label?
Asked Answered
L

2

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?

Laurentia answered 18/8, 2014 at 16:1 Comment(1)
$('#myid input').get(0).nextSibling.nodeValue = 'my new text';Lupus
R
11

Try this:

$('#myid').contents().last().replaceWith('my new text');
Recliner answered 18/8, 2014 at 16:3 Comment(0)
J
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

Jens answered 18/8, 2014 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.