Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  152] [ 1]  / answers: 1 / hits: 40891  / 15 Years ago, thu, january 7, 2010, 12:00:00

I want to hit enter key to go to p2.htm or p3.htm according to the input text that I was typing in. And I also want to hit submit1 button to alert('no1') manually.



It works in FireFox, but in IE6, when I hit enter key it will submit the submit button.



How can I make the thing right in IE 6 as it is in FireFox?



I use javascript and jQuery.



<input id=Text2  type=text  onkeyup=if(event.keyCode==13){go2();} /> 
<input id=Text3 type=text onkeyup=if(event.keyCode==13){go3();} />

<input id=submit1 type=submit value=submit onclick=alert('no1') />

<script type=text/javascript>
function go2()
{
window.location = p2.htm;
}
function go3()
{
window.location = p3.htm;
}
</script>

More From » button

 Answers
16

This function should do the trick:



function submitOnEnter(e, page) {
var key;

if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //Firefox & others

if(key == 13)
window.location = page;
}


You should call it like this:



<input id=Text2  type=text  onkeyup=submitOnEnter(event, 'p2.html') /> 

[#97900] Monday, January 4, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yulisa

Total Points: 436
Total Questions: 102
Total Answers: 123

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
yulisa questions
Sun, Jun 20, 21, 00:00, 3 Years ago
Wed, Apr 14, 21, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Mon, Mar 23, 20, 00:00, 4 Years ago
;