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

Setting form element onClick action with Javascript?

Status
Not open for further replies.

cmayo

MIS
Apr 23, 2001
159
US
Hi all,

I'm having just a heck of a time trying to set the onClick action of a form element using Javascript. Spent most of the morning Googling and all indications are that the code below should work, but it doesn't. I'm probably complicating the issue by coding on a Mac (the only comp available), but on the Mac, I'm getting a "document.getElementById(...) is not an object" error when loading the page.

I've apparently overworked my thinker... can anyone help me out with this?

Thanks much,
Chuck

Code:
<html>
<head>
<script type="text/javascript">
<!--

document.getElementById('x').onclick = function() {alert('beep');}

-->
</script>
</head>

<body>
<form>
<input type="button" name="x" id="x" value="click" />
</form>
</body>
</html>
 
Still laughing at my stupidity... of course you can't reference an object that doesn't yet exist.

I hate to admit that I've been fooling with this for (ahem) four hours, but at least I won't waste another four and can get back to coding... thanks a million, Dan!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top