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. Show
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.
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. In jQuery, you can use the
$(document).ready(function(){ $('#submit').click(function()
{ varhref=
$('a').prop('href'); alert(href); }) }); HTML
Edit in JSFiddle JS
HTML
Edit in JSFiddle 2. Using JavaScriptIn vanilla JavaScript, you can use the JS
HTML
Edit in JSFiddle JS
HTML
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.
How can I get the text value of a clicked link?$('#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.
|