lmk
03-03-2005, 10:36 PM
hi all, I am very new to Javascript and trying to create a javascript quiz which students are answering them by choosing the answers from a dropdown menu, the dropdown menu are created by arrays, so they have common name like QS0, QS1...and so on.
When I tried to submit the answers to a function for checking, I could not get the values over.I have been successful was a text box contect, but could not be done so with a drop menu.
below are the sample code i am working on now before putting in the actual questions.
***
the code works when i make the contect inside the function to read each of the dropdown menu sepearte, but I am trying to make it into an array so I can change the number of questions when required.
many thanks :)
<html>
<head>
<script type="text/javascript">
function put()
{
for (i=0;i<2;i++)
{
txt=eval("document.forms[0].myList" + i + ".options[document.forms[0].myList" + i +.selectedIndex].text")
document.forms[0].answer" + i +".value=txt"
}
}
</script>
</head>
<body>
<form>
Question 1:
<br /><br />
<select name="myList0">
<option value = "1">Answer A</option>
<option value = "2">Answer B</option>
<option value = "3">Answer C</option>
</select>
<br /><br />
<select name="myList1">
<option value = "4">Answer 0</option>
<option value = "5">Answer 1</option>
<option value = "6">Answer 2</option>
</select>
<br /><br />
Your answer 1 is: <input type="text" name="answer0" size="20">
<br /><br />
Your Answer 2 is: <input type="text" name="answer1" size="20">
<br /><br />
<input type="button" name="button1"
value="Display answers"
onClick="put()">
</form>
</body>
</html>
When I tried to submit the answers to a function for checking, I could not get the values over.I have been successful was a text box contect, but could not be done so with a drop menu.
below are the sample code i am working on now before putting in the actual questions.
***
the code works when i make the contect inside the function to read each of the dropdown menu sepearte, but I am trying to make it into an array so I can change the number of questions when required.
many thanks :)
<html>
<head>
<script type="text/javascript">
function put()
{
for (i=0;i<2;i++)
{
txt=eval("document.forms[0].myList" + i + ".options[document.forms[0].myList" + i +.selectedIndex].text")
document.forms[0].answer" + i +".value=txt"
}
}
</script>
</head>
<body>
<form>
Question 1:
<br /><br />
<select name="myList0">
<option value = "1">Answer A</option>
<option value = "2">Answer B</option>
<option value = "3">Answer C</option>
</select>
<br /><br />
<select name="myList1">
<option value = "4">Answer 0</option>
<option value = "5">Answer 1</option>
<option value = "6">Answer 2</option>
</select>
<br /><br />
Your answer 1 is: <input type="text" name="answer0" size="20">
<br /><br />
Your Answer 2 is: <input type="text" name="answer1" size="20">
<br /><br />
<input type="button" name="button1"
value="Display answers"
onClick="put()">
</form>
</body>
</html>