I\'m trying to get href value using jQuery:
Jquery Test
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
if the page have one <a>
It Works,but,many <a>
,have to use var href = $(this).attr('href');
It works... Tested in IE8 (don't forget to allow javascript to run if you're testing the file from your computer) and chrome.
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
**Replacing href**
<div class="cpt">
<h2>
<a href="/ref/ref/testone.html">testoneLink</a>
</h2>
</div>
<h2 class="test" >
<a href="/ref/ref/testtwo.html">testtwoLInk</a>
</h2>
<!--Remove first default Link from href attribut -->
<script>
Remove first default Link from href attribut
$(".geturl a").removeAttr("href");
Add Link to same href attribut
var testurl= $(".test").find("a").attr("href");
$(".geturl a").attr('href', testurl);
</script>