Misc. > Web Development

Click link using JavaScript in Misc.

How to click a hyperlink using JavaScript?


To click the link using JavaScript on web page, we can follow this approach. In this example, we will see that when the button will be clicked, we are imitating the behavior of hyperlink click so that user gets redirected to http://www.dotnetfunda.com.

<p>
    <a id="lnk" href="http://www.dotnetfunda.com">DotNetFunda.com</a>

    <button onclick="ClickLink()">Click Link</button>
</p>

<script>
    function ClickLink()
    {
        document.getElementById('lnk').click();
    }
</script>

When the button is clicked, the ClickLink() JavaScript function is being called (defined in the <script> block).  In this function, we are finding the hyperlink using document.getElementById function and calling click() function that will imitate the behavior of a user cliking on the hyper link and user gets redirected to http://www.dotnetfunda.com website.

 Views: 738 | Post Order: 4



Write for us