<a href="http://www.google.com">link</a>
How do I replace link location from google.com to say "#"? $('a').attr( 'href', '#' );
isn't working.
<a href="http://www.google.com">link</a>
How do I replace link location from google.com to say "#"? $('a').attr( 'href', '#' );
isn't working.
It works for me.
Test code:
<a id="MyLink" href="test.html">
jQuery:
$("#MyLink").attr("href", "#");
alert($("#MyLink").attr("href")); //alerts "#"
Is it possible you are trying to do this before the DOM has loaded?
Also, what browsers are you using?
EDIT:
To ensure this is only done when the DOM is loaded completely, use the document .ready()
function:
$(document).ready(function(){
$("#MyLink").attr("href", "#");
//other initialisation, e.g. event binding
});
Take a look at this question - http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery