How to change href attribute of a hyperlink using jQuery

 2 months ago
source link: https://www.laravelcode.com/post/how-to-change-href-attribute-of-a-hyperlink-using-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to change href attribute of a hyperlink using jQuery


  2 years ago


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 an HTML document from "http" to "https" when the document becomes ready with jQuery.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Set HREF for Anchor Tag</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
            var oldUrl = $(this).attr("href"); // Get current url
            var newUrl = oldUrl.replace("http://", "https://"); // Create new url
            $(this).attr("href", newUrl); // Set herf value
    <p><a href="http://www.google.com">Google</a></p>
    <p><a href="http://www.gmail.com">Gmail</a></p>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK