How to link JS function with HTML? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to link JS function with HTML?

I am always struggling with linking JS function with html ! could you guys please help me to link my awesome money converter? :) <body> <input type="text" id="EUR" value=""> <button oneclick="con()"> Get result</button> <script src="main.js"></script> <div id="msg"> </div> </body> function con(){ "use strict"; var amount = document.getElementById("EUR").value, result = amount * 1.1165, msg = document.getElementById("msg"); } con();

29th May 2019, 3:50 AM
H </07>🔥
H </07>🔥 - avatar
3 Answers
+ 5
Try this bro 😇 <input type="text" id="EUR"> <button onclick="con()">Get Result</button> <div id="msg"></div> <script> function con(){ let result = EUR.value * 1.1165; msg.innerText = result; } </script>
29th May 2019, 8:10 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 5
@Farry I tried whole code over and over !! still doesn't show results <input type="text" id="EUR" value=""> <button oneclick="con()"> Get result</button> <script src="main.js"></script> <div id="msg"> </div> function con(){ "use strict"; var amount = document.getElementById("EUR").value, result = amount * 1.1165, msg = document.getElementById("msg"); } con();
29th May 2019, 5:20 AM
H </07>🔥
H </07>🔥 - avatar
+ 2
add an onclick listener to the button and enclose the js code between script tags
29th May 2019, 4:39 AM
Farry
Farry - avatar