Skip to content

Instantly share code, notes, and snippets.

@dgbrahle
Created July 7, 2010 14:09
Show Gist options
  • Save dgbrahle/466737 to your computer and use it in GitHub Desktop.
Save dgbrahle/466737 to your computer and use it in GitHub Desktop.
Javascript Inputs: Number fix
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script language="javascript" type="text/javascript">
//<![CDATA[
function removeAllAnswers(){
var list = document.getElementById("answer-list");
if(list.hasChildNodes()){
while (list.childNodes.length >= 1){
list.removeChild(list.firstChild);
}
}
};
function insertAllAnswers(){
var answerCount = document.getElementById("jsSelecter").value; //new
var list = document.getElementById("answer-list");
for (var i=1; i<=answerCount;i++) {
var li = document.createElement("li");
var txt = ("Question " + i);
var newtext = document.createTextNode(txt);
document.answer-list.appendChild(newtext);
var input = document.createElement("input");
input.id = "1";
input.name = "1";
input.type = "text";
li.appendChild(input);
list.appendChild(li);
}
};
function resetForm(){
removeAllAnswers();
insertAllAnswers();
}
//]]>
</script>
</head>
<body>
<p>
How many Answers Are needed? <select id="jsSelecter" onchange="resetForm();">
<option selected="selected" value="">
-
</option>
<option value="2">
2
</option>
<option value="3">
3
</option>
<option value="4">
4
</option>
<option value="5">
5
</option>
<option value="6">
6
</option>
</select><br />
</p>
<ul id="answer-list">
<li style="list-style: none">Question 1<br />
</li>
<li>
<input id="1" name="1" type="text" />
</li>
<li style="list-style: none">Question 2<br />
</li>
<li>
<input id="1" name="1" type="text" />
</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment