input - How to select my second radio button in javascript without using id? - Stack Overflow

Hee, I'm working on something but can and dont want to change the HTML. Adding an ID button is not

Hee, I'm working on something but can and dont want to change the HTML. Adding an ID button is not possible. I want to select the second radio input.

This is the html:

  <fieldset>
        <legend>I want to sign up:</legend>
        <label>
            <input type="radio" name="submit-for" value="project">
            <span>For project</span>
        </label>
        <label>
           <input type="radio" name="submit-for" value="stage">
           <span>As intern</span>
       </label>
  </fieldset>

This are the ways i tried selecting it in javascript but it didnt work:

   document.querySelector('input[type="radio"]:nth-of-type(2)').onclick = function () {
       document.getElementById("project").style.display = 'none';
       document.getElementById("stage").style.display = 'block';
   };

and

   document.querySelector('input[type="radio"]:nth-child(2)').onclick = function () {
       document.getElementById("project").style.display = 'none';
       document.getElementById("stage").style.display = 'block';
   };

Can someone please help?

Hee, I'm working on something but can and dont want to change the HTML. Adding an ID button is not possible. I want to select the second radio input.

This is the html:

  <fieldset>
        <legend>I want to sign up:</legend>
        <label>
            <input type="radio" name="submit-for" value="project">
            <span>For project</span>
        </label>
        <label>
           <input type="radio" name="submit-for" value="stage">
           <span>As intern</span>
       </label>
  </fieldset>

This are the ways i tried selecting it in javascript but it didnt work:

   document.querySelector('input[type="radio"]:nth-of-type(2)').onclick = function () {
       document.getElementById("project").style.display = 'none';
       document.getElementById("stage").style.display = 'block';
   };

and

   document.querySelector('input[type="radio"]:nth-child(2)').onclick = function () {
       document.getElementById("project").style.display = 'none';
       document.getElementById("stage").style.display = 'block';
   };

Can someone please help?

Share Improve this question edited Jul 2, 2022 at 9:37 Jonas 129k103 gold badges328 silver badges405 bronze badges asked Nov 3, 2015 at 16:48 Nami92Nami92 551 silver badge5 bronze badges 1
  • document.querySelectorAll('fieldset input[type:radio]').item(1) (be sure to check if it exists, as it could be undefined if there are no two elements) – somethinghere Commented Nov 3, 2015 at 16:50
Add a ment  | 

4 Answers 4

Reset to default 5

Do you really want to select "the second" radio button, or do you want to select "the radio button with value 'stage'"? The second option sounds more extensible.

document.querySelector('input[type="radio"][value="stage"]')

EDIT: Also, upvoted your question for not resorting to ID usage. Always good practice, whether or not you're forced into it.

document.querySelectorAll('fieldset input[type="radio"]')[1]=function () {
  document.getElementById("project").style.display = 'none';
         document.getElementById("stage").style.display = 'block';
    };

You can use document.getElementByName and select the second of the set:

document.getElementsByName("submit-for")[1];

The user @somethinghere was close and the OP chose an answer that did not apply to the title of the question so for those who are looking for the answer to the title of the question here you go:

var r = document.querySelectorAll('input[type="radio"]');
if (r.item.length > 0) {console.log(r.item(1));

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信