Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get href value using jQuery?

People also ask

What method can be used to retrieve the href attribute of an element?

Use getAttribute() to Get Href in JavaScript The Element interface's getAttribute() method returns the value of a specified attribute for the element.

How do you get a href value in BeautifulSoup?

To get href with Python BeautifulSoup, we can use the find_all method. to create soup object with BeautifulSoup class called with the html string. Then we find the a elements with the href attribute returned by calling find_all with 'a' and href set to True .

Can we use value attribute in anchor tag?

you can use custom data attributes see this . <a href="#" data-json="{ 'myValue':'1'}">Click</a> //you can even pass multiple values there.


You need

var href = $(this).attr('href');

Inside a jQuery click handler, the this object refers to the element clicked, whereas in your case you're always getting the href for the first <a> on the page. This, incidentally, is why your example works but your real code doesn't


You can get current href value by this code:

$(this).attr("href");

To get href value by ID

$("#mylink").attr("href");

It's worth mentioning that

$('a').attr('href'); // gets the actual value
$('a').prop('href'); // gets the full URL always

It works... Tested in IE8 (don't forget to allow javascript to run if you're testing the file from your computer) and chrome.


if the page have one <a> It Works,but,many <a> ,have to use var href = $(this).attr('href');