Changing a Link Location

<script>   $("#pageLink").attr("href", "complexpage.html"); </script . . . <body>   <a id="pageLink" href="simplepage.html">Link</a> </body>

With web pages becoming more and more dynamic, even the links that are built into have become dynamic. A great advantage of using jQuery and JavaScript in your pages is that you can easily alter the URL that links point to dynamically based on user input or other sources of data.

To change the URL that a link points to, use the .attr("href", newURL) method. When the user clicks on the link, the browser reads the href attribute of the <a> element and then loads that location as the next page.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.