6/22/2018 - 10:29 PM

jQuery Change href Attribute using .attr() Method

How to change href attribute of a hyperlink using jQuery Answer: Use the jQuery .attr() Method

You can use the jQuery .attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

The following example will show you how to convert all hyperlinks or links in a document from "http" to "https" at runtime with jQuery.


GSP Green Span Profiles



                var oldUrl = $(this).attr("href"); // Get current url

                var newUrl = oldUrl.replace("http://", "https://"); // Create new url

                $(this).attr("href", newUrl); // Set herf value