0

I am currently working on a webpage using html, css, javascript. I implemented several buttons that link to other html-pages. They are supposed to lead to an html page with the name [literally id of the button].html, but I am not sure how to implement this.

For example if the button-id is "a", the button is supposed to link to "a.html" and for the id "b" to "b.html" and so on.

This is my Javascript code for this:

document.addEventListener('DOMContentLoaded', function() {

    // Go to other html-page when button is clicked
    let other_page = document.querySelector('.submit');
    other_page.addEventListener('click', function() {
        location.href = "id.html";
    });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Just get the ID of the button with `other_page.id` and combine the strings with `location.href = \`${other_page.id}.html;\`` – Jacob Nov 12 '22 at 21:13
  • If you have more than one button, then you need to delegate. `document.getElementById("buttonContainer").addEventListener("click", (e) => { const tgt = e.target; if (tgt.matches(".submit")) location = \`${tgt.id}.html\`; });` – mplungjan Nov 12 '22 at 21:22

0 Answers0