Get the href link on click anchor in javascript

Updated at 25-Aug-2021, By samar

Get the href link on click anchor in javascript

In this article, we will see how to solve "Get the href link on click anchor in javascript".

You can get the href link on click of anchor tag in javascript. On click event we can get the href value of the anchor tag and redirect to that link and anything which you want to do with the href link.
  • Get href link on click anchor tag in javascript/jQuery

    <a id="anchorBtn" href="https://w3codegenerator.com"> w3codegenerator </a>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script>
    $('#anchorBtn').on('click', function(e){
        e.preventDefault();
        let href = e.target.href;
        alert(href);
    });
    </script>
    

Back to code snippet queries related jquery