Reputation: 13
I'm trying to create drop down lists when the submit button is pressed. This will go through a while loop to make diffrent number of lists. I currently have the following code but it dosen't work as intended.
<html>
<head>
<script type="text/javascript">
function getvalue() {
number = document.getnumber.input.value;
document.getElementById("result").value = number;
}
</script>
</head>
<body>
<script>
function generatedropdown() {
while (i < number) {
html = '<select name="select" id="i">';
html += '<option>Test 1</option>';
html += '<option>Test 2</option>';
html += '<option>Test 3</option>';
html += '<option>Test 4</option>';
html += '<option>Test 5</option>';
html += '</select>';
innerHTML = html;
i++;
}
}
</script>
<form name="getnumber">Input number:
<input type="text" name="input">
<input type="button" value="Next" onClick="getvalue()">
</form>
<form id="showlists">Number entered:
<input type="text" id="result" readonly="readonly">
<input type="button" value="Show lists" onClick="generatedropdown()">
</form>
</body>
</html>
EDIT: I've changed the code to this but still no luck.
<html>
<head>
<script type="text/javascript">
function getvalue() {
number = document.getnumber.input.value;
document.getElementById("result").value = number;
}
</script>
</head>
<body>
<script>
function generatedropdown() {
html = '<select name="select" id="i">';
while (i < number) {
html+='<option>Test 1</option>';
html+='<option>Test 2</option>';
html+='<option>Test 3</option>';
html+='<option>Test 4</option>';
html+='<option>Test 5</option>';
i++;
}
html+='</select>';
document.getElementById("my-output").innerHTML = html;
}
</script>
<form name="getnumber">
Input number: <input type="text" name="input">
<input type="button" value="Next" onClick="getvalue()">
</form>
<form id="showlists">
Number entered: <input type="text" id="result" readonly="readonly">
<input type="button" value="Show lists" onClick="generatedropdown()">
<div id="my-output">Generated List:</div>
</form>
</body>
</html>
Upvotes: 0
Views: 109
Reputation: 13843
You can use .innerHTML
on elements only, like so:
document.body.innerHTML = 'This is my output';
Now, your body
will be filled with some content. In case you want to put it in an other element, like a div
, you have to create that div
first...
<div id="my-output"></div>
Then get it and apply the content:
document.getElementById('my-output').innerHTML = 'This is my output';
Upvotes: 1
Reputation: 877
html = '<select name="select" id="i">';
while (i < number) {
html+='<option>Test 1</option>';
html+='<option>Test 2</option>';
html+='<option>Test 3</option>';
html+='<option>Test 4</option>';
html+='<option>Test 5</option>';
i++;
}
html+='</select>';
document.getElementById('your_element_id').innerHTML = html;
Upvotes: 1
Reputation: 6381
your syntax is wrong, first of all you have to call getvalue()
before calling generatedropdown()
, then, instead of innerHTML = html;
use document.getElementById('YOUR_CONTAINER_ID').innerHTML = html;
Upvotes: 0