0
How I can make a onClick of a button in JavaScript?
I wanna make a function to a button in my HTML, but I don't know how to make this in JavaScript. I need of help. Some tip?
8 Réponses
+ 3
Rishi Anand told you everything you needed but, to give a simple example of another way of doing it, see here.
https://code.sololearn.com/WdBBSZY8C80r/?ref=app
+ 3
Can you share the code where you tried this? it's kinda hard to say because it all depends on what you want to do when the button is clicked, anyone can suggest a sample code but no one knew what you want to do. So share the code please, or at least elaborate more on what you want to do with the button.
Here's how you share links, in case you didn't know yet:
https://www.sololearn.com/post/74857/?ref=app
+ 3
Eduardo Cavalcante
In case of a simple alert you can simply add this in your document's body.
<button type="button" onclick="alert('Hello world!')">Hello world</button>
+ 2
Use can use add event listener
<button id="btn">click</button>
Now add the following code in JavaScript
document.getElementById("btn").addEventListener("click", function() {alert("hello world!!");});
+ 2
The three ways of adding event listeners (and also why the third way is better) :
https://code.sololearn.com/W0ehmlP2Y3I3/?ref=app
+ 1
Ipang I want to do an alert when the button is clicked. Now can you help me?
+ 1
HTML
<input type="button" onclick="btnClick();" value="click">
Javascript
function btnClick () {
some code
}
- 1
Rishi Anand It's not working yet. What am I supposed to do?