1
<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.

eozzy
  • 66,048
  • 104
  • 272
  • 428

2 Answers2

2

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
  • 29,946
  • 17
  • 95
  • 158
0

Take a look at this question - How to change the href for a hyperlink using jQuery

Community
  • 1
  • 1
Darmen Amanbay
  • 4,869
  • 3
  • 29
  • 50