...

View Full Version : getting enter to behave like onclick button



swidler
03-22-2004, 09:20 AM
i know similar problems have been posted, but i'm new at this and wasn't able to apply the solutions. i have a form with a text box that submits using onclick with a button. when i click on the button, it goes to a function that decides which url should be brought up. when i hit enter, it goes to the url defined in the action= of the form. how do i make enter go to the function?

<form name=gcFormKeywords method=get
action="http://url/script.pl">
<b> </b>
<input type=text name="search" size=30>
<input type=button value="Go" onClick=gcGo() name="button">
</form>

where gcGo is (something like) the following (actually, i've expanded it, but the structure is the same):

function gcGo(searchType) {
var searchType = document.gcFormSearchType.search.selectedIndex;
// index 0 is the keywords option; index 1 is the symbol option; index 2 is the gc id option
// alert(searchType);
// alert(document.gcFormKeywords.search.value);
if (searchType == 1) {
var re = /\*/
if (re.exec (document.gcFormKeywords.search.value)) {
document.gcFormSymStar.symsearch.value =
document.gcFormKeywords.search.value;
document.gcFormSymStar.submit();
}else {
document.gcFormSymbol._symbol.value =
document.gcFormKeywords.search.value;
document.gcFormSymbol.submit();
}
} else{
if (searchType == 2) {
var re = /\*/
if (re.exec (document.gcFormKeywords.search.value)) {
document.gcFormSymStar.symsearch.value =
document.gcFormKeywords.search.value;
document.gcFormSymStar.submit();
}else {
document.gcFormGcid.gc_id.value =
document.gcFormKeywords.search.value;
document.gcFormGcid.submit();
}
}else {
document.gcFormKeywords.submit();
}
}
}

there are 3 or 4 forms after the one quoted above. each sends to a different url.

tia,
naomi

Willy Duitt
03-22-2004, 05:16 PM
Try using your function as the form action.

eg:<form name=gcFormKeywords method=get
action="javascript&#58;gcGo()">

.....Willy

Choopernickel
03-22-2004, 05:34 PM
Bad practice, Willy.

See this:

<form action="http://server.domain/path" onsubmit="return checkForm(event, this);">
<input type="text" name="term" />
<input type="submit" value="Go!" />
</form>

...
<script type="text/javascript">
function checkForm (evt, aForm) {
// run routines on subelements of the 'aForm' object - it's a reference to the form itself
if (/* all validation passes */) {
return true;
}
return false;
}
</script>

swidler
03-22-2004, 05:53 PM
choopernickel-
you lost me a bit. my grasp of js is tenuous at best (i was handed this code and asked to modify it. i managed to add functionality, but i couldn't remove the original bug). can you apply that to my code and tell me what it's supposed to be doing? thanks much.
naomi

Choopernickel
03-22-2004, 09:34 PM
I'd love to, but not with just what you supplied. If you can attach the code that the browser sees (view source instead of attaching from your editor), I'd be happy to take a closer look.

swidler
03-23-2004, 08:24 AM
the code is long, tedious, complex, and (as far as i can tell) the view source code looks identical to the editor code (shouldn't it?). are you wondering about the pieces i didn't include?

Garadon
03-23-2004, 09:29 AM
don't know what browser ur using but both IE and FF fire onclick on both enter and when you click with the mouse

swidler
03-23-2004, 09:33 AM
thanks to everyone. i gave up on the js and got it to work by doing all the processing in a perl script. i'm much more in my element that way....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum