Solved

Javascript cancel Event on Textbox

Posted on 2008-06-10
2
6,562 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently found myself in a Corporate Situation where the client had requested blocking access to any and all websites except his own Domain? Easy? I am sure this would be your answer but their requirement was, this has to be done without using…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now