...

View Full Version : How to start a prompt using the onclick event?



jer2911
02-25-2003, 08:23 PM
I am trying to use a JavaScript prompt to get some information from the user, and then output it to an alert window. The only problem is that the prompt runs immediately upon the loading of the page. Is there a way to use a JavaScript event (possibly such as onclick) to start the prompt, instead of having it run automatically when the page loads? I am hoping for something that in the browser would look like a hyperlink, but when clicking it would start the prompt.

Any help here would be greatly appreciated.

Thanks,
--Brad

beetle
02-25-2003, 08:33 PM
sounds like you've already got it figured out! Have you even tried putting it together?

<a href="#" onclick="alert( prompt( "Whatever", "" ) )">click</a>

jer2911
02-25-2003, 09:45 PM
Yeah, I kinda do have figured out what I want to do, but I don't know how to do it. I think that my problem has to do with how it I have it set up. I am currently "learning" out of a book, and i have it set up something like:

var blah1 = prompt(Question1)
var blah2 = prompt(Question2)
etc...

Then it does a calculation and outputs into an alert box.

(Though since I'm at school right now, I don't have access to my actual code, but I will post it later when I get the chance, if it would be helpful.)

joeframbach
02-25-2003, 10:36 PM
<script...>
function foo()
{
put the onClick code here
}
</script>

<body>
<input type=button onClick="foo()">
</body>

jer2911
02-26-2003, 02:25 AM
Ok, here is the code that I have for my prompt(s):

var player = prompt("What is the player's name?","Player Name");
var natpos = prompt("What is " + player +"'s natural position?","C, 1B, 2B, 3B, SS, LF, CF, RF");
var newpos = prompt("What will be " + player +"'s new position?","C, 1B, 2B, 3B, SS, LF, CF, RF");
var natarm = prompt("What is " + player +"'s approximate Arm rating?","(Enter Rating Here)");
var natrange = prompt("What is " + player +"'s approximate Range rating?","(Enter Rating Here)");
var arm;
var range;

Then it does the calculations and outputs the answer to an alert window.

Beetle: The problem that I have with what said is that I have multiple prompts. (Sorry, I forgot to say that. :( )

joeframbach: Where do I put the script code? In the <head> portion of the code?

Sorry if I'm a complete dummy in this area, I'm just a beginner.

Thank you all for your input so far.

joeframbach
02-26-2003, 03:54 AM
just like that:)

<html>
<head>
<script language="JavaScript">
<!--
function getInput()
{
var player = prompt("What is the player's name?","Player Name");
var natpos = prompt("What is " + player +"'s natural position?","C, 1B, 2B, 3B, SS, LF, CF, RF");
var newpos = prompt("What will be " + player +"'s new position?","C, 1B, 2B, 3B, SS, LF, CF, RF");
var natarm = prompt("What is " + player +"'s approximate Arm rating?","(Enter Rating Here)");
var natrange = prompt("What is " + player +"'s approximate Range rating?","(Enter Rating Here)");
var arm;
var range;
}
-->
</script>
</head>
<body onLoad="getInput()">
</body>
</html>

jer2911
02-26-2003, 07:29 AM
Great, it works when it the page is loaded, but what I really want is for it to run when something is clicked. So what I did was used what you (joeframbach) had given me before for the onclick event thing. It came up with a blank button. This should work. I'm sure I can figure out how to put a name on the button, and if for some reason I can't, I'll just ask again... hehehe.

Thanks a lot!

glenngv
02-26-2003, 09:41 AM
<body>
<form>
<input type="button" value="Get Input" onclick="getInput()">
</form>
</body>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum