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" onclick="document.getElementById('my_id').style.display='block';" >Click Me</a>

<a href="\sompage" onclick="MyFunction();" >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.

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *