0

Can somebody explain where I'm making mistake, please..!

Create a link that says “Buy Tickets” and has a href attribute of “tickets.html”.

Create a button that says “Upgrade”.

When the user clicks “Upgrade”, the “Buy Tickets” link url should change from “tickets.html” to “fancy_tickets.html”.

<script>
document.getElementById("upgrade").onclick = function () {
  document.getElementById("buylink").href = "fancy_tickets.html";
};
</script>

<html>
<body>
<a id="buylink" href="tickets.html">Buy Tickets</a><br>
  <button id ="upgrade">Upgrade</button>
</body>
</html>
TechBantu
  • 15
  • 6

4 Answers4

1

First of all you have a typo in closing <script>, missing /, should be </script>. Then your script is running before the DOM is fully loaded. You can either place the script at the bottom of the body tag or wrap your code with DOMContentLoaded event:

<script>

  window.addEventListener('DOMContentLoaded', (event) => {
    document.getElementById("upgrade").onclick = function () {
      var el = document.getElementById("buylink");
      el.href = "fancy_tickets.html";
      console.log(el.getAttribute('href'));// log the new href value
    };
  });

</script>

<a id="buylink" href="ticket.html">Buy Tickets</a><br>
<button id="upgrade">Upgrade</button>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Like it says in this question, you should use the setAttribute(atr, value); method, something like document.getElementById("buylink").setAttribute("href", "fancy_tickets.html");. Also i suggest you to use addEventListener to listen to events such as onClick

const upgradeBtn = document.getElementById("upgrade");
const buyLink = document.getElementById("buylink");

upgradeBtn.addEventListener('click', () => {
   buyLink.setAttribute("href", "fancy_tickets.html");
});
FLiotta
  • 151
  • 6
  • Hi Fliotta thanks for answering, i understand manipulating that href="tickets.html" attribute, with this code document.getElementById("buylink").href = "fancy_tickets.html"; **This is the part I don't understand..! Create a button that says “Upgrade”. When the user clicks “Upgrade”, the “Buy Tickets” link url should change from “tickets.html” to “fancy_tickets.html”.** – TechBantu Nov 01 '20 at 06:30
  • I tried it after clicking the button it is not taking me to the link fancy_tickets.html – TechBantu Nov 01 '20 at 06:54
0

Your code works well. You forgot to specify the id="upgrade" attribute for the button tag.

document.getElementById("upgrade").onclick = function () {
  document.getElementById("buylink").href = "fancy_tickets.html";
};
<a id="buylink" href="tickets.html">Buy Tickets</a><br>
  <button id="upgrade">Upgrade</button>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

function switch1()
{  document.getElementById("buylink").href = "fancy_tickets.html";
}
<html>
<body>
<a target=_blank id="buylink" href="tickets.html">Buy Tickets</a><br>
  <button id="upgrade" onclick="javascript:switch1();">Upgrade</button>
</body>
</html>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29