views:

199

answers:

2
<a href="http://www.google.com"&gt;link&lt;/a&gt;

How do I replace link location from google.com to say "#"? $('a').attr( 'href', '#' ); isn't working.

+1  A: 

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
});
James Wiseman
Ah yes, how do I delay this until the DOM has loaded completely?
Nimbuz
Use the document ready function. See EDIT above
James Wiseman