avoid onkeyup event on hitting enter key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gatsby
    New Member
    • Jun 2012
    • 2

    avoid onkeyup event on hitting enter key

    I have form which contains a text field and a submit button.
    func_1() is called onsubmit event of the form and func_2() is called onkeyup event of text field.My problem is that func_2() is also called onkeyup event of enter key, I want to avoid calling func_2() on hiting enter key. Is there any better and easier way to do it?? plz suggest me
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use the event.keycode or event.which to get which key was pressed, it they pressed the enter key, don't run the rest of the code.

    Comment

    • gatsby
      New Member
      • Jun 2012
      • 2

      #3
      Code:
      function func_2(str)
      {
      
        $(document).ready(function() {
        $('input[type=text]').on('keyup', function(e) {
        if(e.which == 13) {
         var check=fal;
        }
        })
        });
      	
      if(check==fal){
      	func_1();
      }else
      {
      
      var xmlhttp;
      if (str.length==0)
        { 
        document.getElementById("mydiv").innerHTML="";
        return;
        }
      if (window.XMLHttpRequest)
        {
        xmlhttp=new XMLHttpRequest();
        }
      else
        {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          document.getElementById("mydiv").innerHTML=xmlhttp.responseText;
          }
        }
      xmlhttp.open("GET","target.php?q="+str,true);
      xmlhttp.send();
      }
      }
      this is code for above question. where is mistake here?? and how to fix it?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Aren't you already calling a function on key up? Why are you creating another one? And besides, you can't reference a local variable outside of its function scope.

        Comment

        Working...