-
java script problem
my problem is, I want to check the text field and displays the value in next question. Example,
1. How many chocolates have you had?
ans = 10
(here It has to enter automatically the value to the next question)
2. How may of these 10(it comes automaticaly from the previous question) are having nuts in it?
anyone can you please let me know the code how can i do it in javascript .....
Smily
-
Something like this??...
Code:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function process(){
ans = document.form1.answer.value;
if (document.all){
document.all('question').innerText = '2. How may of these ' + ans + ' are having nuts in it?';
}else if (document.getElementById){
document.getElementById('question').innerText = '2. How may of these ' + ans + ' are having nuts in it?';
}
}
//-->
</script>
</head>
<body>
<form name="form1">
<p id="question">1. How many chocolates have you had?</p>
<input type="text" name="answer"><br>
<input type="button" value="Submit Answer" onclick="process()">
</form1>
</body>
</html>
-
Thanks, But it shows the answer after I click the submit button.
Actually It has to show the answer before submit, b'cos I have nearly 2 questions like this ......Is it possible?
Regards -Smily
-
hi,
Can I use somthing like
document.write(2. How may of these ' + ans + ' are having nuts in it?)
Will it work? I tried but it keeps giving me error :)
Smily
-
Hi,
1/ Its slightly harder to do that. First, you'll have to set the forms method to Get...
Code:
<form name="form1" action="page.htm" method="get">
... so when you submit the form the data is passed through the URL so you should end with something like page.htm?answer=10. Then you can use window.location.search to return answer=10 and use this to as ans.
Or you could try cookies but I'm not really a fan of them.
2/ I think its just because you are missing the extra ' ' s
Code:
document.write(' 2. How may of these ' + ans + ' are having nuts in it?')
-
Thanks, I sorted out this problem with innerHTML
here is my code.....
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form name="form1">
How many chocolates have you had?
<INPUT NAME="chocolates" onBlur="JavaScript:ChocolatesNuts.innerHTML=chocolates.value;">
<p>How many of these <span ID="ChocolatesNuts"></span> are having nuts in it?
</form1>
</body>
</html>