How to get href value using jQuery?

JavascriptJquery

Javascript Problem Overview


I'm trying to get href value using jQuery:

<html>
    <head>
    	<title>Jquery Test</title>
         <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
		$(document).ready(function() {
			$("a").click(function(event) {
				alert("As you can see, the link no longer took you to jquery.com");
				var href = $('a').attr('href');
				alert(href);
				event.preventDefault();
			});
		});
        </script>
    </head>
    <body>
        <a href="http://jquery.com/">jQuery</a>
    </body>
</html>

But it doesn't work. Why?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

You can get current href value by this code:

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

To get href value by ID http://www.url.com"></a>

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

Solution 3 - Javascript

It's worth mentioning that

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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

Solution 6 - Javascript

**Replacing  href attribut value to other** 
 
 <div class="cpt">
   <a href="/ref/ref/testone.html">testoneLink</a>
 </div>

  <div class="test" >
      <a href="/ref/ref/testtwo.html">testtwoLInk</a>
  </div>

 <!--Remove first default Link from href attribut -->
<script>
     Remove first default Link from href attribut
    $(".cpt a").removeAttr("href");
    
    Add  Link to same href attribut
    var testurl= $(".test").find("a").attr("href");
    $(".test a").attr('href', testurl);
</script>

Solution 7 - Javascript

If your html link is like this:

<a class ="linkClass" href="https://stackoverflow.com/"> Stack Overflow</a>

Then you can access the href in jquery as given below (there is no need to use "a" in href for this)

$(".linkClass").on("click",accesshref);

function accesshref()
 {
 var url = $(".linkClass").attr("href");
 //OR
 var url = $(this).attr("href");
}

Solution 8 - Javascript

Assuming you have this html :

   <a class ="linkClass" href="https://stackoverflow.com/"> Stack Overflow</a>

You can get and display the href attribute with this JS snippet :

<script>
    $(".linkClass").click(function() {
        alert($(this).attr("href"));
    });
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdi SembiringView Question on Stackoverflow
Solution 1 - JavascriptGarethView Answer on Stackoverflow
Solution 2 - JavascriptPrince PatelView Answer on Stackoverflow
Solution 3 - JavascriptJahanggir JamanView Answer on Stackoverflow
Solution 4 - JavascriptAlfaTeKView Answer on Stackoverflow
Solution 5 - JavascriptwangtongView Answer on Stackoverflow
Solution 6 - JavascriptHikmat Ullah KhanView Answer on Stackoverflow
Solution 7 - JavascriptSoumya RajivView Answer on Stackoverflow
Solution 8 - Javascriptamd iView Answer on Stackoverflow