+ 2
Javascript help!
I want to perform an action where if the user hits the button, it would display: “I told you not to hit this button!” How can I accomplish this? Thanks. https://code.sololearn.com/WnOHXiJKY2HN/?ref=app
1 Réponse
+ 4
You need to add event listener to the button, listening to the click event.
1. The simplest way is using onclick attribute at the button element.
2. Another way is to use assign onclick callback on the element in JavaScript.
3. The third way is to use addEventListener method.
Check this demo:
Three ways of adding event listeners to elements, by Morpheus
https://code.sololearn.com/W0ehmlP2Y3I3/?ref=app
in
https://www.sololearn.com/post/43430/?ref=app
in
https://code.sololearn.com/Wyr76080kKxS/?ref=app
Due SoloLearn code playground restrictions, you need to enclose your JS scripts in
window.onload = function(){
// .... your JS script
}
Explanation
https://www.sololearn.com/post/90825/?ref=app
in
https://code.sololearn.com/WKKkpq0efxai/?ref=app
A edit of your code, in case you didn't work it out after reading the above.
https://code.sololearn.com/WKb7OT85KEnn/?ref=app