Skip to content

Call href from JavaScript | Example Code

  • by

Using getElementById you can access and assign the href value. Even you can perform click events from script code.

document.getElementById('fo').href = new URL("")

Call href from JavaScript Example

HTML example code: Assign the alert box script in href, you can add URL values also.

The href I’d like to activate is a “javascript:FUNCTION” kind of link.



	<a href="javascript:alert('test');" id="fo">JS Auto Click</a>



	<a href="#" id="fo">JS Auto Click</a>

		document.getElementById('fo').href = "javascript:alert('test');"

       document.getElementById('fo').click();//fake a click on the link



Call href from JavaScript

Do comment if you have any doubts and suggestions on this JS href topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.