{"id":544,"date":"2012-09-21T12:21:00","date_gmt":"2012-09-21T12:21:00","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=544"},"modified":"2026-02-16T16:50:29","modified_gmt":"2026-02-16T16:50:29","slug":"javascript-in-a-link","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/links-javascript\/javascript-in-a-link","title":{"rendered":"Javascript in a link"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Running javascript in a link<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Just using javascript when clicked<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"javascript:void(0);\" onclick=\"document.getElementById('my_id').style.display='block';\" &gt;Click Me&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<p>You can also use this, but the need for the &#8220;javascript:&#8221; marker is arguably annoying when adding multiple javascript commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"javascript: document.getElementById('my_id').style.display='block';\" &gt;Click Me&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using javascript in addition to a href<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"\\sompage\" onclick=\"document.getElementById('my_id').style.display='block';\" &gt;Click Me&lt;\/a&gt;\n\n&lt;a href=\"\\sompage\" onclick=\"MyFunction();\" &gt;Click Me&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using multiple javascript commands<\/h5>\n\n\n\n<p>Just add them one after the other<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"javascript:void(0);\" onclick=\"document.getElementById('my_id1').style.display='block'; document.getElementById('my_id2').style.display='none'; my_function(); \"&gt;Click Me&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using href=&#8221;#&#8221; or href=&#8221;javascript:void(0)&#8221; to do nothing<\/h4>\n\n\n\n<p>href=&#8221;javascript:void(0)&#8221; is usually the better choice.<\/p>\n\n\n\n<p>href=&#8221;#&#8221; causes page to scroll to the top of the document unless you use &#8220;return false&#8221; in whatever javascript function is called.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running javascript in a link Just using javascript when clicked You can also use this, but the need for the &#8220;javascript:&#8221; marker is arguably annoying when adding multiple javascript commands: Using javascript in addition to a href Using multiple javascript commands Just add them one after the other Using href=&#8221;#&#8221; or href=&#8221;javascript:void(0)&#8221; to do nothing [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-links-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":12,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":4388,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/544\/revisions\/4388"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}