...

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



DooM_MonkeY
06-25-2004, 02:25 AM
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)

danman
06-25-2004, 03:30 AM
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)


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum