Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to start a prompt using the onclick event?

    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

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sounds like you've already got it figured out! Have you even tried putting it together?

    <a href="#" onclick="alert( prompt( "Whatever", "" ) )">click</a>
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.)

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    <script...>
    function foo()
    {
    put the onClick code here
    }
    </script>

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

  • #5
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    --Brad

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    just like that
    Code:
    <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>

  • #7
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    --Brad

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    <body>
    <form>
    <input type="button" value="Get Input" onclick="getInput()">
    </form>
    </body>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •