Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

isDefined not recognized if they hit enter to submit 1

Status
Not open for further replies.

peterswan

Programmer
Sep 25, 2002
263
US
Hello,

The "isDefined" function is not working on my action page when the user hits enter to submit, and the button is not selected. So if the user types in the info and hits enter, it goes to the action page but nothing happens. If they either hit tab to select the button or click on the button, it works fine. I need to use "isDefined" because there are two ways to get to the action page.

Is there any way I can get the isDefined function to work if they hit enter, even if the submit button is not selected?

Thanks to any and all with advice.

Cheers [smile] [smile]

Peter
 
Yes. You need to use some JavaScript to define the variable if it is not defined, or you can redesign the page. I'm assuming that the name of the variable in question is the value of the NAME attribute of the Submit button. In my examples, I'll use the variable "foo" with value "bar".

Redesigning the page might be easiest: If the variable always has the same value when that page is submitted, you can re-write the page like this:

<form ...>
<input type=&quot;text&quot; ... >
<input type=&quot;button&quot; onClick=&quot;this.form.submit();&quot;>
<input type=&quot;hidden&quot; name=&quot;foo&quot; value=&quot;bar>
</form>

If the variable's value differs depending what the user does of what the values typed in are, a solution would be to use the onSubmit attribute of the <FORM> tag to set the value of the hidden variable. (You still need a hidden variable to ensure that the variable is defined and because you must have something to set the value of.

Both of the previous techniques will work in IE and Netscape. For a more complex page, you could capture the keypress event; and, if the user pressed Enter and he was on such-and-such field, set the value of the variable appropriately. The JavaScript code for doing that would be different for different browsers.
 
Thanks flowcontrol,

As you can see, Wullie's answer worked fine in my case. Still, I always wondered what would happen if there were several submit buttons within on form on a page, and the user went down to a textbox below the first submit button, entered in some text and hit &quot;enter&quot;. The first submit button would submit, not the second, because by default it's the one that's selected.

Your last paragraph gave a great solution to this. What I would do is write some Javascript to change the value of the variable if they type something in to a textbox, and ask if that variable is defined.

Thanks, [smile] [smile]

Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top