[ic] Odd behavioir when adding items to cart

Peter peter at pajamian.dhs.org
Thu Nov 10 20:15:56 EST 2005


On 11/10/2005 09:49 AM, DB wrote:
> If you click the buy button instead of pressing Enter, all seems well.
> Can this odd behavior be changed, or at least have no action take place
> when Enter is pressed thus requiring the buy button to be clicked?

It is possible to capture and disable the Enter keypress with 
javascript, or alternatively to set the appropriate buy submit button 
and then submit the form.  The javascript is rather complex and i'm not 
about to spend the good 2 or 3 hours to figure it all out properly for 
this particular form.  But I will show you some hacked together 
(completely untested) code that should work to disable the keypress for 
one text input box.  I'll leave the rest of it up to you to figure out:

Say you've got a form with a text input box that looks like this:
<form method="post" action="blahnextpageblah" name="myForm">

...

<input type="text" name="quantityX" size="2">

...

</form>

You create a javascript function that is called via the onload attribute 
to the body tag, something like this (note suffers from word wrap):

function onLoad() {
   // Register the keypress event
   if (document.myForm.quantityX.captureEvents) 
document.myForm.quantityX.captureEvents(Event.KEYPRESS);
   document.myForm.quantityX.onkeypress = killOffEnter;
}

Note that in the above you'd want to modify it to loop through and add 
the keypress event to each field that has a name that begins with 
quantity.  That is pretty easily done, but I don't have the time to 
remember it or figure it out right now.

...and another function which kills off the enter key (by basically 
doing nothing but telling the browser that it handled the enter keypress 
event):

function killOffEnter(e) {
   if (!e) var e = window.event;
   if (e.keyCode) var code = e.keyCode;
   else if (e.which) var code = e.which;
   if (code == 13) {
     // You can put some code in here to figure out which
     // field had enter pressed, then set the appropriate
     // name/value pair to make it work as if the buy
     // button was clicked and submit the form.
     return false;
   }
   return true;
}

Peter


More information about the interchange-users mailing list