Replace part of an HREF attribute in a link with jQuery

Jon picture Jon · Oct 16, 2010 · Viewed 16.9k times · Source

I'm new to jQuery and I need to replace a part of an A HREF attribute in a link. More specifically, a piece of code that will remove the "-h" from "s1600-h" for a lightbox application:

In other words, turn s1600-h to s1600

Also, do I need to use $(function() or $(document).ready(function() before the piece of code?

Answer

Bang Dao picture Bang Dao · Oct 16, 2010
$(document).ready(function(){
    $('a').each(function(){
        this.href = this.href.replace('s1600-h', 's1600');
    });
});