Bogey Posted October 2, 2012 Share Posted October 2, 2012 (edited) 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 October 2, 2012 by Bogey Link to comment Share on other sites More sharing options...
Ingolme Posted October 2, 2012 Share Posted October 2, 2012 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 More sharing options...
Bogey Posted October 2, 2012 Author Share Posted October 2, 2012 YES...!!! that did it!!! #@$#@$ I tried this: ....onclick = test(), but not without the bracets... THNX!!!!! Link to comment Share on other sites More sharing options...
ShadowMage Posted October 3, 2012 Share Posted October 3, 2012 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 More sharing options...
Bogey Posted October 3, 2012 Author Share Posted October 3, 2012 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now