aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Enter Key event working fine in IE but not FireFox. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Enter Key event working fine in IE but not FireFox." Watch "Enter Key event working fine in IE but not FireFox." New topic
Author

Enter Key event working fine in IE but not FireFox.

Krish Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 58
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.......




Thanks & Regards
KITTU
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I have no idea what document.activeElement.name is. Is that actually working in Firefox. What is it alerting?

Eric
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Sep 17, 2008
Posts: 58
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Sep 17, 2008
Posts: 58
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

If you just need to know which element triggered the event, that's in the event instance object.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enter Key event working fine in IE but not FireFox.