Jump to content

add Event to <DIV>


Bogey

Recommended Posts

Hi all, This line of code is not working:

document.getElementById(divID).onclick="test()";

What I am trying to do is, when a JSfunction is called, this function adds a event to a particular DIV I tried it on different manners f.e. I replaced the test() for the function itself, instead of calling it... This on the other had DOES work!

document.getElementById(targetIDreact).title="testing"; document.getElementById(targetIDreact).style.cursor="pointer";document.getElementById(targetIDreact).className="DbContentContainer";

Any help?

Edited by Bogey
Link to comment
Share on other sites

With this code, all you're doing is setting a property called "onclick" to a string of value "test()" What you need is to actually reference the function. Since it is a reference and not an execution, it must not have parenthesis.

document.getElementById(divID).onclick= test;

Link to comment
Share on other sites

This is usually the next question asked after being shown how to assign a function by reference so I'll answer it before it's asked: If you want to pass parameters to a function you'll need to wrap the function call in what's called an anonymous function:

document.getElementById(divID).onclick = function() { test(aParameter); }

An anonymous function is basically a function without a name.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...