Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user input without prompt window 1

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
CA
In C if I wanted to do this I'd use a gets or scanf statement. Is there an equivelent in JS?

I have a loop, and within the cycle I want input from the user. I don't want to use a prompt. I'd prefer it to be mouse driven if possible.

Ideally, I'd like it to perform the actions within the loop, wait for the user to choose one of two options (represented by buttons) and repeat the loop.

Possible? All I know how to do are prompts. As always, you time and help is appreciated.
 
This code will prompt the user with four different questions and store them in an array for later processing. Don't know how this will interact with your loop (it may prevent your needing it). If this doesn't give you an idea about how you can use JavaScript to solve your problem, you might post a little more info about what you're trying to accomplish. I didn't take the time to insure that this is cross-browser-capable, but hopefully it's a start for you.

Good luck.


<html>
<body onload=&quot;getData()&quot;>
<script language=&quot;javaScript&quot;>
var Questions = new Array(&quot;Question 1&quot;, &quot;Question 2&quot;,
&quot;Question 3&quot;, &quot;Question 4&quot;);
var NextQuestion=0;
var Responses = new Array();
var ResponseCount=0;
var IntervalID;

function getData()
{
//populate the text box with the first question
//this could be left out if you want to initialize the value
//with simple HTML
document.getElementById('prompt').innerHTML = Questions[NextQuestion];
IntervalID = setInterval('checkData()', 500);
}


function checkData()
{
var cBoxes = document.forms['myForm'].elements['input'];

for (var i=0; i < cBoxes.length; i++)
{
if (cBoxes.checked == true)
{
//store the response
Responses[ResponseCount] = cBoxes.value;
ResponseCount++;

//reset the button
cBoxes.checked = false;

//show the next question
if (NextQuestion+1 < Questions.length)
{
NextQuestion++;
document.getElementById('prompt').innerHTML = Questions[NextQuestion];
}
else
{
document.getElementById('prompt').innerHTML = &quot;No more questions&quot;;
//stop listening
clearInterval(IntervalID);
alert(Responses);
}
}
}


}
</script>
<form method=&quot;POST&quot; name=&quot;myForm&quot;>
<table border=&quot;1&quot; width=&quot;100%&quot;>
<tr>
<td width=&quot;100%&quot; id=&quot;prompt&quot;> </td>
</tr>
<tr>
<td width=&quot;100%&quot; > <input type=&quot;radio&quot; value=&quot;1&quot; name=&quot;input&quot;>
Selection 1    <input type=&quot;radio&quot; name=&quot;input&quot; value=&quot;2&quot;>
Selection 2</td>
</tr>
</table>
</form>
</body>
</html>
 
Thank you so much. I've never met a bunch of people who are willing to go so far out of their way to help. This is an incredible resource. Greedyzebra, I wish there is more I could do than give you a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top