Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enter Key event working fine in IE but not FireFox.

 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I need one requirement that if user wants to perform search by using Enter Key event.
In case when focus is set on "search text box" at that time enter key event will be captured and perform the search and fetch the results on GUI.
But if the focus is set on the other GUI elements and if user hit the enter key in that case one alert mesaage will be displayed and again user hit the enter key the alert message should get closed.

I wrote a function that is working fine for IE browser but when I tested using Mozilla(FireFox) version 3.0 in that browser once alert message is displayed and again hit the enter key the alert message displays repeatedly.

My code is like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<SCRIPT LANGUAGE="JavaScript">
<!--
alert('inside js');
document.onkeyup = KeyCheck;

function refreshPageForFilter()
{
alert ("Search For text box");
}


function KeyCheck(e)
{
alert('inside 1st function');
var KeyID =(window.event) ? event.keyCode : e.keyCode;
//var KeyID = event.which;

if(KeyID == 13)
{
// alert("element name...after enter key....."+document.activeElement.name);
if(document.activeElement.name != "t1" )
{
alert("Click the appropriate button or link for the desired action you want.");
}
else
{
refreshPageForFilter();
}
}
else
{
return ;
}
}


//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM METHOD=POST ACTION="" name="frm1">
<INPUT TYPE="text" NAME="t1">Search For

<INPUT TYPE="text" NAME="t2">test2

<INPUT TYPE="text" NAME="t3">test3

</FORM>

</BODY>
</HTML>

Can any body help me regarding this.......



 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what document.activeElement.name is. Is that actually working in Firefox. What is it alerting?

Eric
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about your actual problem (do you want to submit the form on press of the enter key, or do you want to display an alert on press of the enter key), but this doesn´t look good. A form by default already submits itself to the specified action on press of the enter key in one of the input fields. If you want to display an alert message during submitting the form, you could use the onsubmit attribute of the HTML form element for that.
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric & Bauke,
Thanks for your reply

Hi Bauke,
I want to alerting the messages when I press enter key when the focus is in text boxes. In IE it is working fine but in firefox it is showing the problem at ---- if(document.activeElement.name != "t1" ){ }
There is submit button on the form actually. The form contains simply 3 textboxes, the alert message when 1st textbox is got focused and the alert message the 2nd/3rd text boxes got focussed is different. Can you suggest me according to this please what I have to do?







 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah OK, I see.

Well, document.activeElement is part of upcoming HTML5 and not supported in older browsers. FireFox 3 should support it however. Maybe you was using FireFox 2? In such case you could add a helper function which crawls the DOM tree (or only the DOM forms tree) and finds the element which has focus, then return it. It is the same as the activeElement in HTML5.
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bauke,
I am new to the javascript. Can you please post me the bit of code that how I can solve the mentioned javascritp problem?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just need to know which element triggered the event, that's in the event instance object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic