Solved

Javascript cancel Event on Textbox

Posted on 2008-06-10
2
6,568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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 +…

691 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