[JavaScript] How to Use a href="" onclick

Tadashi Shigeoka ·  Wed, October 12, 2011

While researching how to use onclick, I found an article called “The Bad Practice of href=”#” onclick” so I’ll introduce it.

JavaScript

Methods Using a Tags

Bad Example

Search

If there’s a value in the href attribute of an a element, it will try to reference the href attribute value after executing the onclick attribute content.

Also, if # is included, it will navigate to the top of the page.

Good Example

Search

Also, if you’re going to execute an onclick event handler, this writing style is clean and good:

Search

Method Using span as Alternative

Search

That’s all.

Reference Information

That’s all from the Gemba.