Solved

How to use onkeydown in Javascript

Posted on 2010-11-12
19
654 Views
Last Modified: 2012-05-10
Dear Experts,

Just want to how to dectect enter key after users input product No: in inputbox.
When users press enter key, the URL will redirect to other link accroding to inputbox
product NO and display page in second frame in the same window of inputbox.
After redirection is completed, the inputbox will be clear for users to be ready
for next edit product No:. So the question, how to write a code for onkeydown to
dectect enter key and do clear inputbox value for user next edit ?

THe following javascript code is finished  inputbox, frame set for URL redirect, onclick clear vlaue operation but except final one for detecting enter key to clear  input box vlaue for next product
NO edit



Please advise
Ducan
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing Input Box</title>

<TITLE>Document Title</TITLE>
<script>
function formSubmit(frm)
{
      document.getElementById("frame1").src="http://www.mywebsite.com/"+frm.text1.value+".htm";
onkeydown="if (event.keyCode==13) {frm.text1.value=''}" 
      return false;
}

</script>

</HEAD>
<body>

<style>
.highlighted { background-color:white; }
</style>

<form onsubmit="return formSubmit(this)">
<input type="text" name="text1" value="0000" onclick="this.value=''; this.className='highlighted'" 
    onblur="if(this.value=='')this.value=this.defaultValue; this.className=''"  /> 
<input type="submit" name="submit" >
</form>
<iframe height="700" width="1400" scrolling="no" border="0px" id="frame1" src="http://www.mywebsite.com/index.htm">
</body>

Open in new window

0
Comment
Question by:duncanb7
  • 11
  • 5
  • 2
19 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34119346
Take a look at this:

http://www.w3schools.com/jsref/event_onkeydown.asp

The keyCode for Intro if I remember well is 13
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119427
I think I could not use onkeydown because there is onclick and onsubmit operation, and I need those two
operation is finished and detecting last key of enter key, so I should put detecting ocde in the function
formSubmit(frm), Right ? but the following code is not working because I don't know what is the event

function formSubmit(frm)
{
      document.getElementById("frame1").src="http://www.mywebsite.com/"+frm.text1.value+".htm";
if (event.keyCode==13) {frm.text1.value=''};
      return false;
}


 
0
 
LVL 10

Expert Comment

by:TiberiuGal
ID: 34119493
Hi,
you can use onkeydown without a problem. But the formSubmit function doesn't make sense to me.
here's how I'd write it
<script type="text/javascript">
// e - event, el - element, fn- callback function
function checkEnter(e, el) {

    var key=e.keyCode || e.which;
    if (key==13){
       fn();
       return false;
    }
    return true;
}

function formSubmit() {
   document.getElementById("frame1").src="http://www.mywebsite.com/"+ frm.text1.value +".htm";
}
</script>

<!--  there's no need of a form if you handle it via an iframe -->
<input type="text" ...
onkeydown="return checkEnter(event, this, formSubmit);" />

<input type="button" value="submit" onclick="formSubmit();" />

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 10

Expert Comment

by:TiberiuGal
ID: 34119504
Fix:
0
 
LVL 10

Expert Comment

by:TiberiuGal
ID: 34119537

<script type="text/javascript">
// e - event, el - element, fn- callback function
function checkEnter(e, el, fn) {

    var key=e.keyCode || e.which;
    if (key==13){
       fn();
       return false;
    }
    return true;
}

function formSubmit() {

  var my_val = frm.text1.value;
  // check if value is empty
  if ( my_val.length > 0) { 
    document.getElementById("frame1").src="http://www.mywebsite.com/"+ my_val +".htm";
  }

}
</script>

<!--  there's no need of a form if you handle it via an iframe -->
<input type="text" ...
onkeydown="return checkEnter(event, this, formSubmit);" />

<input type="button" value="submit" onclick="formSubmit();" />

Open in new window

0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34119549
onKeyDown fires everytime a user press a key within the focused object.

If I understand what you want, you only need to make a function that fires on product number keyDown where you recover the value on product number input, change the action of your form according to this readed value when keyCode pressed is 13 and finally submits the form.

0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119566
Sorry the point is going to expert, TiberiuGal,  and I will re-credit in since I score it too fast
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119583
http://www.experts-exchange.com/Community_Support/General/Q_26610487.html
Sorry again, I already submit re-credit form to EE adm
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119665
Dear TiberiuGal,

When delete <form onsubmit="return formSubmit(this)"> and then
redirect to www.mywbeiste.com/66666.htm is failed. it is getting worse
than the previous thread

Please advise.

Duncan
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119676
onkeydown="return checkEnter(event, this, formSubmit);" />

to be repacled by onkeydown="return checkEnter(event, this, formSubmit()   );" />

Is it correct ?
0
 
LVL 10

Expert Comment

by:TiberiuGal
ID: 34119690
Hi,
I see, the function formSubmit references the form, we need to change that reference by adding an id attribute to the text box
function formSubmit() {

  var my_val = document.getElementById('text1').value;
  // check if value is empty
  if ( my_val.length > 0) { 
    document.getElementById("frame1").src="http://www.mywebsite.com/"+ my_val +".htm";
  }

}

<input type="text" name="text1" id="text1" ...

Open in new window

0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119752
Okay it is back to previous thread status,  for example,

Edit 66666 and Press enter ,and redirect it is sucessful t0 .../66666.htm but
66666 number is still in input box not clear so user needs to click again for next edit.

So, the checkenter is not done yet , Please advise
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119768
Actually you understand my question ?
Do i need to write it again
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119771
I mean after enter key is pressed,
formSubmit()  function activate first or do first  for redirect, and then check enter key operation is next
to clear input box so user doesn't need to click the input box again for next edit
0
 
LVL 10

Accepted Solution

by:
TiberiuGal earned 500 total points
ID: 34119772
I missed that part :

function formSubmit() {

  var my_val = document.getElementById('text1').value;
  
  // check if value is empty
  if ( my_val.length > 0) { 
    document.getElementById("frame1").src="http://www.mywebsite.com/"+ my_val +".htm";
  }
  document.getElementById('text1').value = '';
}

Open in new window

0
 
LVL 13

Author Comment

by:duncanb7
ID: 34119811
Yes, your are right, I also
put el.value='' that is also working
 
if (key==13){
       fn();
el.value='";
       return false;
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34122260
Dear TiberiuGal,

Could you comment this new thread at
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26611155.html ?
please advise

Duncan
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 34125695
Thanks for all of our reply

If interest , please go to this new thread
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26611155.html
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

830 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