How to get clicked anchor tag value in javascript

This is a list representation of data which is coming from a php database.The list is fetched using a foreach php loop and the data inside the achor tag is populated accordingly.


 

As you can see here, inside the anchor tag i have href-ed it to a modal box which will show a message whenever the user clicks on any of the link. There is a button in the modal window which will take the user to a different page. The issue is that i want to pass certain value along with the url, but cannot do so as the link to the next page is defined in the modal window specification part. So i came up with this solution. I thought of using the id attribute of the anchor tag, I tried to get the id attribute of the anchor which was clicked using javascript.


From this i wanted to initialize a php variable and then use that php variable to pass as value in the href of the modal window button. But the value i am getting in the alert box is 'undefined' for some reason. I have tried all possible combinations of this.

$('a',this).attr('id');
$this.id;

Everything return 'undefined'.

Reference-This is the code for the modal window part.


This post will discuss how to get the href value of an anchor tag in JavaScript and jQuery.

1. Using jQuery

In jQuery, you can use the .prop() method to get the href value of an anchor tag. This is demonstrated below:

JS


$(document).ready(function(){

    $('#submit').click(function() {

        varhref= $('a').prop('href');

        alert(href);

    })

});

HTML


    href="https://www.bing.com/">Click Me

    

    


Edit in JSFiddle

 
To get the text of an anchor element, use the .text() method.

JS


$(document).ready(function(){

    $('#submit').click(function() {

        vartext= $('a').text();

        alert(text);

    })

});

HTML


    href="https://www.bing.com/">Click Me

    

    


Edit in JSFiddle

2. Using JavaScript

In vanilla JavaScript, you can use the querySelector(), which will return the first anchor tag within the document. Then we can directly access the href property to get the exact value of the href attribute. The following code demonstrates this.

JS


document.getElementById('submit').onclick= function(){

    varhref= document.querySelector('a').href;

    alert(href);

}

HTML


    href="https://www.bing.com/">Click Me

    

    


Edit in JSFiddle

 
This can also be done using the getAttribute() method, which gets the value of the href attribute on the specified anchor tag.

JS


document.getElementById('submit').onclick= function(){

    varhref= document.querySelector('a').getAttribute('href');

    alert(href);

}

HTML


    href="https://www.bing.com/">Click Me

    

    


Edit in JSFiddle

That’s all about getting the href value of an anchor tag in JavaScript and jQuery.


Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding 🙂


How do I find the value of an anchor tag?

Get href value of an anchor tag with JavaScript/jQuery.
Using jQuery. In jQuery, you can use the . prop() method to get the href value of an anchor tag. ... .
Using JavaScript. In vanilla JavaScript, you can use the querySelector() , which will return the first anchor tag within the document..

Can I use onclick in anchor tag?

Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside tag: This method create a button inside anchor tag.
$('#my-div a'). click(function() { var txt = $(this). text(); console. log(txt); });

How do I hit an anchor tag in JavaScript?

Click() function is used when you want to trigger click event. ie. when you want to trigger some action when anchor tag is clicked.