4

I'm trying to create 10 radio buttons, labeled 1-10 with a for loop inside of my html and I can't get it to work.

<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <script>
                for (var i = 1; i <=10; i++) {
                    <input type="radio" name="scores" id="i" value="i"> i
                }
            </script>
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
</html>

Thanks. I'm new to JS so I'm still learning a lot.

1
  • JS isn't like PHP. You can't write directly to the markup, you have to go through the DOM using either the document object or using a library like jQuery. Commented Apr 28, 2016 at 17:02

5 Answers 5

6

Use document.write to output the HTML like so.

document.write('<input type="radio" name="scores" id="i" value="i"> i');

<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <script>
                for (var i = 1; i <=10; i++) {
                    document.write('<input type="radio" name="scores" id="i" value="i">'+ i);
                }
            </script>
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

end of the document.write line should read value="i"> ' + i);
Thank you, I knew I missed something!
3

You can add all inputs to one string inside loop and then append it to HTML, also you should separate your js and html code

var inputs = '';
 for (var i = 1; i <= 10; i++) {
   inputs += '<input name="scores" type="radio" value="' + i + '" id="' + i + '">'+i+'';
 }
 document.getElementById('NPSform').insertAdjacentHTML('afterbegin', inputs);
<h2 style="text-align:center">On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?</h2>
<form id="NPSform" style="text-align:center">
  <input type="submit" name="mysubmit" value="Submit" />
</form>

You can also create one div, set innerHTML and use insertBefore to add it to html

var inputs = '';
for (var i = 1; i <= 10; i++) {
  inputs += '<input name="scores" type="radio" value="' + i + '" id="' + i + '">' + i + '';
}

var div = document.createElement('div');
div.innerHTML = inputs;

var submit = document.querySelector('#NPSform input');
submit.parentNode.insertBefore(div, submit);
<h2 style="text-align:center">On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?</h2>
<form id="NPSform" style="text-align:center">
  <input type="submit" name="mysubmit" value="Submit" />
</form>

1 Comment

Should you specify the script tag should be placed at the end of the body or at least somewhere outside the form?
0
<html>
<head>
<script>
  function addbuttons() {
    var buttons = document.getElementById("dynamicButtons");
            for (var i = 1; i <=10; i++) {
                buttons.innerHTML += '<input type="radio" name="scores" id="' +i +' " value="' +i +'">'
            }

            }
 </script>
</head>
<body onload="addbuttons()">
  <h2 style="text-align:center">
    On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
  </h2>
    <form id="NPSform"; style= "text-align:center">     
        <div id="dynamicButtons"></div>
        <input type="submit" name="mysubmit" value="Submit"/>
    </form>
</body>
</html>

Comments

0
<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
    <script>
        for (var i = 1; i <=10; i++) {
            document.write("<input type='radio' name='scores' id='"+i+"' value='"+i+"'> "+i+"");
        }
    </script>
</html>

1 Comment

I suggest you to improve your example by reading the Minimal, Complete and verifiable example.
-1
<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
      <form id="NPSform"; style= "text-align:center">

        <div id="radioscontainer">
        </div>

        <input type="submit" name="mysubmit" value="Submit"/>
      </form>


<script>
  var radioButtons = '';
  for (var i = 1; i <=10; i++) {
    radioButtons += '<input type="radio" name="scores" id="i" value="i"> i';
  }//for
  $("#radioscontainer").append( radioButtons );
</script>
</body>
</html>

1 Comment

He might need a pure js approach instead of jQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.