javascript - Modify the text of my radio input button? - Stack Overflow

Your average radio button has a name and value, but also some text next to it, in this case "Chang

Your average radio button has a name and value, but also some text next to it, in this case "Change this text."

How can I change this in javascript? Or even alert it? AFAIK, it is NOT the .inner html.

HTML:

            <input type="radio" name="radioOption1" value="Array 1"> Change this text

Javascript

    var confirmIExist = document.getElementsByName("radioOption1");
alert(confirmIExist.innerHTML);
    //alerts undefined

If it's not .innerHTML, what is it? if I grab the input object with either getElementByName or getElementById, what chunk after that represents the Alert text?

Your average radio button has a name and value, but also some text next to it, in this case "Change this text."

How can I change this in javascript? Or even alert it? AFAIK, it is NOT the .inner html.

HTML:

            <input type="radio" name="radioOption1" value="Array 1"> Change this text

Javascript

    var confirmIExist = document.getElementsByName("radioOption1");
alert(confirmIExist.innerHTML);
    //alerts undefined

If it's not .innerHTML, what is it? if I grab the input object with either getElementByName or getElementById, what chunk after that represents the Alert text?

Share Improve this question edited Jul 2, 2022 at 10:06 Jonas 129k103 gold badges328 silver badges405 bronze badges asked Aug 12, 2013 at 4:55 user2448657user2448657 1111 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You could use alert(confirmIExist[0].nextSibling.textContent), but wouldn't it be better to place the text next to the radio button in a <label> and then get the inner html of that

<input type="radio" id="radioOption1" name="radioOption1" value="Array 1"><label for="radioOption1" id="r1option">Change this text</label>
...
var label = document.getElementById("r1option");
alert(label.innerHTML);

You cannot set inner html for a input element. instead wrap your text with a and give it a ID

<input type="radio" name="radioOption1" value="Array 1"> <label id="radioText">Change this text</label>

input is a self-closing element. It cannot have innerHTML

nextSibling will return the Node that follows the radio button.

document.getElementsByName('radioOption1')[0].nextSibling.nodeValue = 'Text changed';

jsFiddle Demo

confirmIExist[0].nextSibling.textContent="abc"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745282229a4620343.html

相关推荐

  • javascript - Modify the text of my radio input button? - Stack Overflow

    Your average radio button has a name and value, but also some text next to it, in this case "Chang

    17小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信