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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onKeypress Error 1

Status
Not open for further replies.

Coogan

Programmer
Jun 21, 2004
38
GB
Hi Folks,

I have been working on getting a small bug sorted on my site an I am having some troubles.

What I am trying to do is, if someone presses enter in a textbox I want it to perform an action, like open another page.

The function code is:

Code:
<script language="JavaScript"><!--
function handler(e) {
    if (document.all) {
        e = window.event;
    }
    
    var key;

    if (document.layers)
        key = e.which;
    if (document.all)
        key = e.keyCode

if(key=13)window.open( this.form.url.value);

}
//--></script>

The code that is calling the function is:

Code:
<input name="url"  type="text" onKeyPress="handler()">

When ever I press any key in the text box I get an error saying:

"Error: 'this.form.url' is null or not an object"

Please can some one tell me where I am going wrong??
 
Try this as a replacement to your handler function. The only thing you need to change is the form name:

Code:
function handler(eventData) {
	var keyCode = eventData ? eventData.keyCode : event.keyCode;
	if (keyCode == 13) window.open(document.forms['yourFormName'].url.value, '', '');
}

Hope this helps,
Dan
 
Dan,

Many thanks. It worked a treat!!

Cheers

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top