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?
With jQuery, you can use the text() method to replace the text of an anchors tag with a specific text.
We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.
Definition and Usage. The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!
$(document).ready(function(){
$('a').each(function(){
this.href = this.href.replace('s1600-h', 's1600');
});
});
jQuery(document).ready(function() {
jQuery('a').each(function(){
this.href = this.href.replace('s1600-h', 's1600');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With