how to call various javascript function into html | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

how to call various javascript function into html

i have create a various js code with function. i want to call those function into html. how can i do that

21st Sep 2020, 3:14 PM
Dove
Dove - avatar
45 Answers
+ 10
Look. Here’s the answer You make a button. That button looks like this: <button onPress=“dostuff()”>CLICK HERE></button> That means that on button press, whatever is in the quotes is Js and is runned. Now, in the JS code write: function dostuff() { //PUT ANY JS CODE YOU WANT HERE } the function do stuff is just a short way of saying everything that’s where ”//PUT ANY JS CODE YOU WANT HERE” is printed understand?
22nd Sep 2020, 12:56 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 7
You can use HTML element attributes, for example: <button onclick=handleClick()>Click me</button>
21st Sep 2020, 3:22 PM
Bagon
Bagon - avatar
+ 7
Ipang what of something like this <!DOCTYPE html> <html> <body> <h1>The onclick Event</h1> <p>The onclick event is used to trigger a function when an element is clicked on.</p> <p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p> <button onclick="myFunction()">Click me</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; } </script> </body> </html>
21st Sep 2020, 3:42 PM
Dove
Dove - avatar
+ 6
You need the script tag.
21st Sep 2020, 10:20 PM
Sonic
Sonic - avatar
+ 5
♠👑 ℍⓄlʸᵇ𝕆𝒾 👍♦ check the JS tutorial. As already mentioned, the way you do it on code playground is different from when you write code outside SL.
21st Sep 2020, 10:38 PM
Sonic
Sonic - avatar
21st Sep 2020, 10:49 PM
Sonic
Sonic - avatar
+ 5
♠👑 ℍⓄlʸᵇ𝕆𝒾 👍♦ Use event attributes to call them in html (search about it on google) But I personally don't prefer to call functions in html! I prefer to use event listeners!
21st Sep 2020, 10:53 PM
Namit Jain
Namit Jain - avatar
+ 5
Galaxy-Coding (CodeWarrior) now think on when calling from diff file. you make much sense.thanks bro
22nd Sep 2020, 1:00 PM
Dove
Dove - avatar
+ 5
Include Js file in HTML then call the function by inline or event call.
22nd Sep 2020, 3:16 PM
Programmer Rushikesh
Programmer Rushikesh - avatar
+ 4
You can use a button to call your JS function <button type="button" onclick="fun()">call function fun</button>
21st Sep 2020, 3:21 PM
Ipang
+ 4
♠👑 ℍⓄlʸᵇ𝕆𝒾 👍♦ In SoloLearn HTML, CSS and Javascript are written into sections in Code Playground editor. But if we work outside SoloLearn, HTML, CSS and Javascript codes can be written into separate files. Importing CSS or Javascript makes sense only when we work outside SoloLearn.
21st Sep 2020, 4:07 PM
Ipang
+ 3
Ipang explain in detail pls
21st Sep 2020, 3:24 PM
Dove
Dove - avatar
+ 3
if that above code is correct. Ipang two question arise there (1) how can i import and execute js there. (2)how can that be automatic. if not explain deeper
21st Sep 2020, 3:45 PM
Dove
Dove - avatar
+ 3
♠👑 ℍⓄlʸᵇ𝕆𝒾 👍♦ There are detailed answers to your question already as far as I understand your question. If you need more help or want something explained in more detail, please also give more details on what we can help you with and where exactly you're stuck and what you have tried. The script you sent for Ipang works as intended I guess. What was your question there? What do you mean by import and execute? And what do you want to be automatic?
21st Sep 2020, 9:41 PM
Martin Möhle
Martin Möhle - avatar
+ 3
Sonic show an example pls,go to previous comment and see the code i made and then import js function for me
21st Sep 2020, 10:32 PM
Dove
Dove - avatar
+ 3
Sonic how sir
21st Sep 2020, 10:39 PM
Dove
Dove - avatar
+ 3
You can use script tag to load js code in html file and you can use event attributes like on click , on drag
22nd Sep 2020, 10:03 AM
Master Genius [ INACTIVE ]
Master Genius [ INACTIVE ] - avatar
+ 3
<body onload="brython()"> <div id="board"></div> </body> like this then how do we connect the script with the js code
22nd Sep 2020, 11:19 AM
Dove
Dove - avatar
+ 3
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.7.1/brython.min.js"> </script> <script> this is the script
22nd Sep 2020, 11:23 AM
Dove
Dove - avatar
+ 3
Your script tag should be in head otherwise it will give error because, js must load before it is used
22nd Sep 2020, 11:42 AM
Master Genius [ INACTIVE ]
Master Genius [ INACTIVE ] - avatar