Running javascript in a link

Just using javascript when clicked
<a href="javascript:void(0);" onclick="document.getElementById('my_id').style.display='block';" >Click Me</a>

You can also use this, but the need for the “javascript:” marker is arguably annoying when adding multiple javascript commands:

<a href="javascript: document.getElementById('my_id').style.display='block';" >Click Me</a>
Using javascript in addition to a href
<a href="\sompage#something" onclick="document.getElementById('my_id').style.display='block';" >Click Me</a>
Using multiple javascript commands

Just add them one after the other

<a href="javascript:void(0);" onclick="document.getElementById('my_id1').style.display='block';" document.getElementById('my_id2').style.display='none'; my_function(); ">Click Me</a>

Using href=”#” or href=”javascript:void(0)”

href=”javascript:void(0)” is usually the better choice.

href=”#” causes page to scroll to the top of the document unless you use “return false” in whatever javascript function is called.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.