Mohamed Saligh Mohamed Saligh - 27 days ago 7
HTML Question

Can I have onClick() event for <h1> to <h6> tags?

I want to have onClick() event on

<h1> to <h6>
(any of heading tags) for running some javascript. Please give me some example to have onClick() event in
which shows some alert() message.

Thanks :)

Answer Source

It's called the inline event registration model. And yes, you can do more or less what you're asking after.

But please don't do this.

It's ancient and reliable, yes. But the drawback is that it requires you to put your JS behaviors into your XHTML structure.

You are much better off using the following idiom:

element.onclick = doSomething;

In jQuery, this might look something like:

     alert('Handler for .click() called.')

Good luck!