Solved

Javascript cancel Event on Textbox

Posted on 2008-06-10
2
6,565 Views
Last Modified: 2013-12-07
I am developing an autosuggest control on a textbox. I have some events assigned to the textbox for auto suggest. When I press enter on the textbox I want it to perform something else. But the enter event fires the form submit on the form inside which it is located.

I tried to cancel the event propogation but the form is submitted anyway. The form does not have any onSubmit event on it. Is there a way I can stop the form from submitting when I press enter.

Here is how I initialize the autosuggest textbox.

AutoSuggestControl.prototype.init = function () {
      var oThis = this;
      this.textbox.onkeyup = function (oEvent) {
            if (!oEvent) {
                  oEvent = window.event;
            }  
             if(oEvent.keyCode == 13) {
         oEvent.cancelBubble = true;
            if (oEvent.stopPropagation) oEvent.stopPropagation();
         oEvent.cancel = true;
            oEvent.returnValue = false;
      }
            oThis.handleKeyUp(oEvent);
      };
      this.textbox.onkeydown = function (oEvent) {
            if (!oEvent) {
                  oEvent = window.event;
            }    
        if(oEvent.keyCode == 13) {
         oEvent.cancelBubble = true;
            if (oEvent.stopPropagation) oEvent.stopPropagation();
         oEvent.cancel = true;
            oEvent.returnValue = false;
      }
            oThis.handleKeyDown(oEvent);
      };
      this.textbox.onblur = function () {
            oThis.hideSuggestions();
      };
      this.textbox.onfocus = null;
      this.createDropDown();
};

in Key down handler I have

AutoSuggestControl.prototype.handleKeyDown = function (oEvent /*:Event*/) {
      switch(oEvent.keyCode) {
            case 38: //up arrow
                  this.previousSuggestion();
                  break;
            case 40: //down arrow
                  this.nextSuggestion();
                  break;
            case 13: //enter
                  alert(oEvent.cancelBubble);
                var recName = this.textbox.value
                   var recId = 0;
                   for (var i=this.startPos; i <=this.endPos; i++) {
                  if(recName == this.objNameArr[i] ) {
                        recId = this.idArr[i];
                        break;
                        }
                  }
                  selectObject(recId, recName, this.viewPageId, this.inputName, this.isMultiple);
                  this.hideSuggestions();
                  break;
      }
};

When I press enter in the textbox, the form is getting submitted no matter what type of event cancel code I have used.
Could anyone point out where I am going wrong?
0
Comment
Question by:zmoidin
2 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 21754780
It looks like you need to call preventDefault too.

http://developer.mozilla.org/en/docs/DOM:event.preventDefault
0
 

Author Closing Comment

by:zmoidin
ID: 31465916
thanks that seemed to be the needed one.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question