Replace a URL using jQuery

The problem: I need to replace a url on one page, but that url was defined in the master page of an asp.net app.

The solution: Use the jQuery .attr (attribute) selector to find the matching href attribute I want to replace. Here’s the one line of code that does the trick:

JavaScript
1
$("a[href]='http://mysite.com/old']").attr('href', 'http://mysite.com/new' )

Result: Mind. Blown. Instant awesomeness without needing to rebuild the app.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>