Forum Moderators: open

Message Too Old, No Replies

Grabbing form values for JavaScript

         

Jeremy_H

8:50 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



Hello,

I'm having a devil of a time trying to grab a value from my form and running it in a script.

I've simplified my form, and posted it below. Any ideas? Anybody see my typo or problem? Right now, the alert comes back undefined.

Thanks

------

<html>
<head>
<script type="text/javascript" language="javascript">
var TestVar = document.q.q101.value;
function check(){alert(TestVar);return false;}
</script>
</head>

<body>
<form method="post" id="q" name="q" onSubmit="return check()">
<input type="radio" name="q101" value="y"><input type="radio" name="q101" value="n">
<button type="submit">Next</button>
</form>
</body>
</html>

DrDoc

8:55 pm on Mar 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would, because TestVar is not a dynamic pointer. Instead, in your example, it gets defined while the page is still in process of loading, but prior to the existance of the form itself. I would recommend changing your code a tad, perhaps to something like:

<html>
<head>
<script type="text/javascript" language="javascript">
function check(which){
var TestVar = which.q101.value;
alert(TestVar);
return false;
}
</script>
</head>

<body>
<form method="post" id="q" name="q" onSubmit="return check(this)">
<input type="radio" name="q101" value="y"><input type="radio" name="q101" value="n">
<button type="submit">Next</button>
</form>
</body>
</html>

Jeremy_H

9:10 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



Thanks DrDoc.

I didn't realize the value was being defined right away, I thought since it didn't run until on pressed the submit button, that it would work.

I tried using the modified script you provided, but I still had issues with it. I do not totally understand values in the parentheses, so I don't know if I have to change it from what was provided. (ie; do I use check(which) and check(this), or something else?)

Thanks

Jeremy_H

12:04 am on Mar 3, 2006 (gmt 0)

10+ Year Member



Duh, I get it.

I just need to put the definition of the variable inside the function!

Thanks.