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?

27th Oct 2019, 1:40 PM
Eduardo Cavalcante
Eduardo Cavalcante - avatar
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
27th Oct 2019, 2:17 PM
Russ
Russ - avatar
+ 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
27th Oct 2019, 1:50 PM
Ipang
+ 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>
27th Oct 2019, 2:21 PM
Ipang
+ 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!!");});
27th Oct 2019, 1:47 PM
Rishi Anand
Rishi Anand - avatar
+ 2
The three ways of adding event listeners (and also why the third way is better) : https://code.sololearn.com/W0ehmlP2Y3I3/?ref=app
29th Oct 2019, 12:05 PM
Gordon
Gordon - avatar
+ 1
Ipang I want to do an alert when the button is clicked. Now can you help me?
27th Oct 2019, 2:02 PM
Eduardo Cavalcante
Eduardo Cavalcante - avatar
+ 1
HTML <input type="button" onclick="btnClick();" value="click"> Javascript function btnClick () { some code }
27th Oct 2019, 6:24 PM
Alexander Lebedev
Alexander Lebedev - avatar
- 1
Rishi Anand It's not working yet. What am I supposed to do?
27th Oct 2019, 2:01 PM
Eduardo Cavalcante
Eduardo Cavalcante - avatar