Proper way to have an anchor tag with onclick event

Anchor tags are one of the most common HTML tags used in pages. If the user right clicks on the anchor tag he/she will be provided with an option of 'Open in new Window/Open in new Tab'. This will happen only if the anchor has href attribute.

Sometimes it is required to do some other action (like making ajax request) on click of the anchor link and also provide the right click option for the same anchor.

Natural practice by web developers.

<a href="javascript:void(0);" onclick="alert('i am here');">Click Me! </a>

Click Me without link for anchor!


Correct way to add anchor tags without losing anchor properties.

<a href="http://www.blogger.com/amareswar.blogspot.com" onclick="alert('i am here'); return false;">Click Me! </a>

Click Me!

In the above link, you can observe that the user can right click on go to the respective link and onclick will do the custom action provided by the developer.

Comments

Unknown said…
Thank you Amareswar Reddy. I was looking for this solution. and you helped me.
Appreciate your help.

-jana
navya said…
Wow. This really made my day. Thanks a lot!

JavaScript Training in CHennai JavaScript Training in CHennai JQuery Online Training JQuery Online Training
HTML5 CSS3 JavaScript Training in Chennai | HTML5 Online Training
Unknown said…
its good for understanding about html, anchor tag digital marketing company in india
Mounika said…
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
PoL said…
real estate template WordPress. A responsive website design crafted specifically for real estate. Download it now on TemplateMonster.
Anu said…
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
David Fincher said…
This post is so interactive and informative.keep update more information...
Android Training in Anna Nagar
Android Training in Chennai
Matt Reeves said…
This post is so interactive and informative.keep update more information...
DevOps course in Tambaram
DevOps Training in Chennai
milka said…
Great post. keep sharing such a worthy information.
rest api testing training in chennai
api testing training

Popular posts from this blog

Some thoughts on MongoDB Nodejs driver

CORS issues with IE9 and workarounds