Can someone help debug this, it's for a prank

Question:

why is my js and html not working?
Repl link:

https://replit.com/@royanxu/e?v=1

code snippet

Are you referring to the buttons?

Welcome to the community, @royanxu!

In order to let a button run code when it is clicked, you need to assign an id to the button:

<!-- I have assigned "my-button" as the id -->
<button id="my-button">Click Me!</button>

Then, you will need to let the button run some code by adding an event listener using the HTMLElement.addEventListener() function. This can be implemented like this:

// Gets the button element
const myButton = document.getElementById("my-button");

// Adds the click event listener
myButton.addEventListener("click", function() {
    // This is where you want to your code to run whenever the button is pressed. Here, I changed the button text using innerHTML as an example
    myButton.innerHTML = "I have been clicked!";
});

Alternatively, you can also use the HTMLElement.onclick attribute and assign a function to it:

// Gets the button element
const myButton = document.getElementById("my-button");

// Assigns the onclick attribute to a function
myButton.onclick = function() {
    // Insert any code you want to run here. I have done the same as the one above here
    myButton.innerHTML = "I have been clicked!";
};
4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.