最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I change a label text without interfering with an input radio inside the label? - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 11

Try 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论