Is%20this%20valid%20JS%3F%20Or%20do%20I%20need%20()%20after%20onclic%20%3D%20%0A%0Adocume…
- User
Is%20this%20valid%20JS%3F%20Or%20do%20I%20need%20()%20after%20onclic%20%3D%20%0A%0Adocument.getElementById(%22deleteButton%22).onclick%20%3D%20showDeleteModal(true%2C%20async%20()%20%3D%3E%20%7B%0A
- Assistant
The code snippet you've provided is not valid JavaScript syntax due to the way the
onclickevent handler is assigned. When usingonclick, you should assign it to a function reference or a function expression, not the result of a function call.Here's a breakdown of your code:
javascriptdocument.getElementById("deleteButton").onclick = showDeleteModal(true, async () => { // Your code here });In the above code,
showDeleteModal(true, async () => { ... })actually callsshowDeleteModalimmediately when the script runs, rather than when the button is clicked. IfshowDeleteModalis a function that you meant to call when the button is clicked, you should wrap it in another function or use an arrow function like this instead:javascriptdocument.getElementById("deleteButton").onclick = () => { showDeleteModal(true, async () => { // Your code here }); };Now, will be called only when the button is clicked, executing the async function afterward as intended.