Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call onclick for an HTMLAnchorElement - Capture Enter Key

Status
Not open for further replies.

jaybytez

Programmer
Jan 8, 2003
65
0
0
US
We have a situation where we have developers defined an onkeypress event for the body tag that checks to see if the enter key was pressed and if so it does a click() method against an anchor tag that we use as a submit button. Now I have another field and anchor tag that I would like to be able to submit with the enter key. The problem is that I can capture the event and try to handle it, but in DOM, the HTMLAnchorElement does not have an onclick...that seems totally weird to me. It only has an onBlur and onFocus. So my javascript works in IE, but not Mozilla or Firefox. Anyone have any suggestions for how to call a click()/onclick() event for a HTMLAnchorElement.

-jay
 
what? the [tt]<a>[/tt] object [tt]certainly[/tt] has an onclick event.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
That page doesn't list events, just properties and methods. If you click on the link that says: "The anchor element. See the A element definition in HTML 4.0." you will see the onclick event listed there under "Attributes defined elsewhere".


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
good eye ts, thought I was insane for a second.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Okay...cool. So here is some code. It works in IE and not Mozilla/Firefox...Any suggestions.

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<script>
var firstOnKeyRequest = 0;

function onkeypressSubmit( fieldName )
{
	firstOnKeyRequest += 1;
	if( firstOnKeyRequest == 1 )
	{
		document.getElementById(fieldName).click();
	}
}
</script>
<body onkeypress="if (event.keyCode == 13) onkeypressSubmit('searchButton');">
<form>
	<input type="text" id="text1" onkeypress="if (event.keyCode == 13) onkeypressSubmit('headerSearchWidget');"/>
	<a href="#" id="headerSearchWidget" onclick="alert('headerSearchWidget')">Go</a>
</form>
<form>
	<input type="text" id="text2" />
	<a href="#" id="searchButton" onclick="alert('searchButton')">Search</a>
</form>
</body>
</html>

In Mozilla/Firefox...I get the following error

Code:
Error: document.getElementById(fieldName).click is not a function
Source File: file:///C:/Sample1.html
Line: 20

I really appreciate your help on this because I have a bug due today on this.

-jay
 
Try:

Code:
document.getElementById(fieldName).[red]on[/red]click();

If that doesn't work, I've rewritten the code to work. Let me know.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Looks like that worked...thanks.

I would be interested to see how you changed the code though. What was getting me side tracked is it appears Mozilla works but Firefox sorta works. It brings the alert window up but logs a javascript error:

Code:
Error: [Exception... "'Permission denied to get property XULElement.selectedIndex' when calling method: [nsIAutoCompletePopup::selectedIndex]"  nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)"  location: "JS frame :: <unknown filename> :: onclick :: line 0"  data: no]

I would like to see your code though, to see if you implemented it cleaner.

Thanks for your help!!!

-jay
 
cLFlaVA: I thought you were insane too. No! I mean I thought I was insane too.

After making a JS boo-boo here earlier this week, I just had to go and check that one out for myself. It just didn't seem possible for there to be no onclick event for the very first thing in html that you could click on!


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
here's the code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script language="javascript"><!--
var firstOnKeyRequest = 0;

function onkeypressSubmit( fieldName ) {
    firstOnKeyRequest += 1;
    if( firstOnKeyRequest == 1 )
        document.getElementById(fieldName).onclick();
}

function doTest(e, d) {
  if (e.keyCode == 13) onkeypressSubmit( d );
}
--></script>

</head>

<body onkeypress="doTest(event, 'searchButton');">

<form>
    <input type="text" id="text1" onkeypress="doTest(event, 'headerSearchWidget');" />
    <a href="#" id="headerSearchWidget" onclick="alert('headerSearchWidget'); return false;">Go</a>
</form>
<form>
    <input type="text" id="text2" />
    <a href="#" id="searchButton" onclick="alert('searchButton'); return false;">Search</a>
</form>

</body>
</html>

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top