13

Possible Duplicate:
How to change the href for a hyperlink using jQuery

I have a link that is being generated by a database that I need to change.

I was wondering if I can use jQuery to find the required id and change the href value?

Community
  • 1
  • 1
patrick
  • 659
  • 3
  • 9
  • 25

4 Answers4

31

Sure:

$("#linkId").attr("href", "http://the.new.url");
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
16

If your HTML was like this:

<a href="xxx" id="myLink">whatever</a>

You can change the link by doing this in plain javascript:

document.getElementById("myLink").href = "http://whatever.com/yyyy";

Or, using jQuery:

$("#myLink").attr("href", "http://whatever.com/yyyy");
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Yes.

<script>
$(function() {
  $("#id_of_link").attr("href", "/new/url");
});
</script>
regality
  • 6,496
  • 6
  • 29
  • 26
0

If you want to do this when the page loads, use the following code

$(document).ready(function() {

$("#linkId").attr("href", "http://the.new.url");

});
codingbadger
  • 42,678
  • 13
  • 95
  • 110