?
Solved

Javascript cancel Event on Textbox

Posted on 2008-06-10
2
Medium Priority
?
6,570 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Several part series to implement Internet Explorer 11 Enterprise Mode
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Suggested Courses

762 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