I'm experimenting with changing the href
link in JavaScript. Specifically, I want to switch the URL path from "linktwo.html" to "problem3.html".
While there may be simpler ways to achieve this, I'm using this as a learning exercise.
In my sample code below, what mistake am I making in attempting to alter the href
?
HTML:
<body onload="changeLink()">
<div id="p">
<h1><a href="linkone.html">first link</a></h1>
</div>
<div id="q">
<a href="linktwo.html">second link</a>
</div>
Javascript:
<script type="text/javascript">
function changeLink() {
document.getElementById("q").querySelector('a').href = "problem3.html";
}
</script>