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 2 of 2
  1. #1
    New Coder
    Join Date
    May 2004
    Location
    Some place called the Earth I think...
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question: how can i hav the alert say the value of an input type="text" ?

    ok, the title kinda said it all, I'm trying to make a validation type thing i guess and I want an alert box to say what the user typed in. I have this and I know it isnt right:

    <html>
    <head>
    <script>

    var ppj;

    function check(theText) {

    if(theText.value == ppj)
    alert("2 + 2 = "+ppj);
    }
    </script>
    </head>

    <body>
    <input type="text" id="bob" value="" /><button onclick="check('bob')">Check</button>
    </body>
    </html>

    (p.s "ppj" isnt supposed to mean anything I just was hungry and thought of penut butter and jelly. lol)
    Dont do drugs, get high on life

    13 years and getting nowhere fast.....


    đ­˘M_M÷˝kŔą

  • #2
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From the code you posted, I can't tell exactly what you want the jscript to do, but it looks like you want the function to check the value of a text box against a variable and spit out an alert if the value of the text box and the variable are the same. If that's the case, there are a couple of problems with your code.

    First: ppj is a blank variable. You didn't give it any value. (It might be better not to use it at all).
    Second: you pass the check function the variable "bob". "bob" has no value. You must use the document.getElementById function to pass the textbox object to the function.
    Here's how: (Note--this code will do nothing if the value of the text field is not the same as ppj)
    Code:
    <html>
    <head>
    <script>
    
    var ppj=4;
    
    function check(theText) {
    
    if(theText.value == ppj)
    alert("2 + 2 = "+theText.value);  //you may also use ppj here if the values are equal
    }
    </script>
    </head>
    
    <body>
    <input type="text" id="bob" value="" /><button onclick="check(document.getElementById('bob'))">Check</button>
    </body>
    </html>


  •  

    Posting Permissions

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