Add an onclick event to a div?

Yes … although it should be done with caution. Make sure there is some mechanism that allows keyboard access Build on things that work.

Is it possible to add onclick to a div and have it occur if any area of the div is clicked. Yes … although it should be done with caution. Make sure there is some mechanism that allows keyboard access.

Build on things that work If yes then why is the onclick method not going through to my div. You are assigning a string where a function is expected. DivTag.

Onclick = printWorking; There are nicer ways to assign event handlers though, although older versions of Internet Explorer are sufficiently different that you should use a library to abstract it. There are plenty of very small event libraries and every major library (such as YUI or jQuery) has event handling functionality.

– OVERTONE Aug 2 at 9:46 If there were parentheses then it would be a function call and the return value would be assigned instead of the function itself. – Quentin Aug 2 at 9:47 That makes so much sense now. I've done that multiple times elsewhere in my code and had no idea what was going on.

– OVERTONE Aug 2 at 9:56.

Assign the onclick like this: divTag. Onclick = printWorking; The onclick property will not take a string when assigned. Instead, it takes a function reference (in this case, printWorking).

The onclick attribute can be a string when assigned in HTML, e.g. , but this is generally not recommended.

Everythings works well. You can't use divtag. Onclick, becease "onclick" attribute doesn't exist.

You need first create this attribute by using .setAttribute(). Look on this reference.sitepoint.com/javascript/Eleme... . You should read documentations first before you start giving "-".

It is allowed, and it can be done, as proven in this example. jsfiddle.net/VYSna/ It didn't work for you because you tried to pass the function printWorking()() to the onClick handler, instead don't add the parenthesis, since javascript takes the reference to the function.

That's a string, not a function. – Quentin Aug 2 at 9:47 Got mixed up with the quotes, corrected :P – Truth Aug 2 at 9:59 Where on earth did printWorking()() come from? – Quentin Aug 2 at 10:01 when trying to pass printWorking() as the handler, javascript interpreted it as printWorking()().

– Truth Aug 2 at 10:03 It would be printWorking()(event) when the event fired, if the question was passing printWorking(), which it isn't. – Quentin Aug 2 at 10:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions