how to call various javascript function into html | SoloLearn: Learn to code for FREE!

+5

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

js

9/21/2020 3:14:21 PM

snail movement

46 Answers

New Answer

+9

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?

+6

You can use HTML element attributes, for example: <button onclick=handleClick()>Click me</button>

+6

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>

+5

You need the script tag.

+4

♠👑 ℍⓄ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.

+4

Check out this link: http://www.simplehtmlguide.com/javascript.php

+4

♠👑 ℍⓄ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!

+4

Galaxy-Coding (CodeWarrior) now think on when calling from diff file. you make much sense.thanks bro

+4

Include Js file in HTML then call the function by inline or event call.

+3

You can use a button to call your JS function <button type="button" onclick="fun()">call function fun</button>

+3

♠👑 ℍⓄ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.

+2

Ipang explain in detail pls

+2

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

+2

♠👑 ℍⓄ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?

+2

Sonic show an example pls,go to previous comment and see the code i made and then import js function for me

+2

Sonic how sir

+2

You can use script tag to load js code in html file and you can use event attributes like on click , on drag

+2

<body onload="brython()"> <div id="board"></div> </body> like this then how do we connect the script with the js code

+2

<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.7.1/brython.min.js"> </script> <script> this is the script

+2

Your script tag should be in head otherwise it will give error because, js must load before it is used