Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <h1> which shows some alert() message.

Thanks :)

like image 348
Mohamed Saligh Avatar asked Nov 28 '10 06:11

Mohamed Saligh


1 Answers

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:

  $(":header").click(function(){
     alert('Handler for .click() called.')
   });

Good luck!

like image 53
Joseph Weissman Avatar answered Oct 15 '22 19:10

Joseph Weissman