+ 1

What is the code to make a button when clicked will show some text

28th May 2020, 10:37 AM
⚛️⚛️👨‍🔬👨‍💻 ADHIKSIT 👨‍💻👨‍🔬⚛️⚛️
⚛️⚛️👨‍🔬👨‍💻 ADHIKSIT 👨‍💻👨‍🔬⚛️⚛️ - avatar
3 odpowiedzi
+ 4
For that you need Javascript click event. Make a function who does whatever you want, and then give the click event to your button.
28th May 2020, 10:44 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 3
<div id="sample"> <button type="button" onclick="myfun()"> <script type="text/javascript"> function myfun(){ document.getElementById("sample").innerHTML = "the text will you want to print after clicking the button"; } </script> This code will definitely work.
28th May 2020, 11:35 AM
Dinesh Kuniyal
Dinesh Kuniyal - avatar
+ 1
Adhiksit Got ya! A click event is raised whenever an element is clicked. To define your own handler function to handle the click event do this. //get the button var btn = document.getElementById('btn); btn.addEventListener('click', function(){ //Do something on click console.log('clicked'); }); Or simply do btn.onClick = function(){ //Do something on click console.log('clicked'); } https://www.sololearn.com/learn/JavaScript/2758/
28th May 2020, 12:25 PM
Ore
Ore - avatar