Wednesday, 2 May 2018

How to change anchor tag href attribute of a hyperlink using jQuery


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set HREF for Anchor Tag</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('a[href^="http://"]').each(function () {
            var oUrl = $(this).attr("href"); // Get the current url
            var nUrl = oUrl.replace("http://", "https://"); // new url
            $(this).attr("href", nUrl); // Set herf new url
        });
    });
</script>
</head>
<body>
    <p><a href="http://www.google.com">Google</a></p>
    <p><a href="http://www.gmail.com">Gmail</a></p>
</body>
</html>

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...