Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6578
  • Last Modified:

Javascript cancel Event on Textbox

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
zmoidin
Asked:
zmoidin
1 Solution
 
David S.Commented:
It looks like you need to call preventDefault too.

http://developer.mozilla.org/en/docs/DOM:event.preventDefault
0
 
zmoidinAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now