473,378 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

avoid onkeyup event on hitting enter key

2
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
Jun 9 '12 #1
3 2489
Rabbit
12,516 Expert Mod 8TB
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.
Jun 10 '12 #2
gatsby
2
Expand|Select|Wrap|Line Numbers
  1. function func_2(str)
  2. {
  3.  
  4.   $(document).ready(function() {
  5.   $('input[type=text]').on('keyup', function(e) {
  6.   if(e.which == 13) {
  7.    var check=fal;
  8.   }
  9.   })
  10.   });
  11.  
  12. if(check==fal){
  13.     func_1();
  14. }else
  15. {
  16.  
  17. var xmlhttp;
  18. if (str.length==0)
  19.   { 
  20.   document.getElementById("mydiv").innerHTML="";
  21.   return;
  22.   }
  23. if (window.XMLHttpRequest)
  24.   {
  25.   xmlhttp=new XMLHttpRequest();
  26.   }
  27. else
  28.   {
  29.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  30.   }
  31. xmlhttp.onreadystatechange=function()
  32.   {
  33.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  34.     {
  35.     document.getElementById("mydiv").innerHTML=xmlhttp.responseText;
  36.     }
  37.   }
  38. xmlhttp.open("GET","target.php?q="+str,true);
  39. xmlhttp.send();
  40. }
  41. }
this is code for above question. where is mistake here?? and how to fix it?
Jun 10 '12 #3
Rabbit
12,516 Expert Mod 8TB
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.
Jun 10 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Trent | last post by:
Hi. I know the basic way to assign event handlers: <input onKeyUp="processEvent(event)" /> But how do I assign a function to the onKeyUp event in *javascript* that can access the event...
2
by: Evan Wong | last post by:
I have problem to get onkeyup event. If we put the event in HTML statement like - "<input name=field1 size=16 onkeyup="javascript:function1();>" it works. If we put in JavaScript code like...
2
by: 23s | last post by:
My site's login page has a form w/ 2 textboxes and a submit button. If I'm in either of those textboxes (i.e., either one of the textboxes has focus), in any given browser, hitting "enter" on my...
2
by: john.lum | last post by:
My overall objective is to create something akin to Google Suggest, where a query is done in response to changes in a text field presented to the user. I've got things working using the onkeyup...
1
by: manojsharma | last post by:
hey can anybody tell me y my onkeyup event is not working in firefox.. it is working fine in IE... code <input type="text" name="oddteam" size="1" value="1" ...
2
by: Sunfire | last post by:
I was wondering if there was a way to hit enter to insert blank lines while in the designer? I tried this but all it seems to want to do is insert code that really shouldn't be there. For example,...
3
by: Brent | last post by:
The code below correctly sets an iframe to be editable, and then tries to attach an "onkeyup" event to the iframe. Instead of doing that, however, it generates a cryptic "Object required" error in...
0
by: user1980 | last post by:
How do I write a custom validator for the onkeyup event. my scenario is two textboxes and if one of them is filled the other has to be filled to. if not both need not be filled(so, onkeyup event...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.