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

Using the onClick event without having an onClick attribute in HTML?

Status
Not open for further replies.

deadpool42

Programmer
May 24, 2004
40
I want to insert some code into the onClick event of various form elements. The problem is that the form is being generated by something else, so I can't just put an onClick attribute into the appropriate tags. I seem to remember being able to do this, but when I use this code,

Code:
document.form.element.onClick = alert("test");

the alert appears when the page loads and not when you click the element.
 

You need to do either assign to a function name, or to a new function you create on-the-fly:

Code:
document.forms['formName'].elements['elementName'].onclick = myClickHandler;

function myClickHandler(e) {
   alert("test");
}

Code:
document.forms['formName'].elements['elementName'].onclick = function(e) {
   alert("test");
}

Hope this helps,
Dan

 
I knew it would end up being something stupid like that. I actually had it as a function and was just using the alert to test it. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top