Execute a javascript function with html button? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Execute a javascript function with html button?

How would I execute this external javascript function with html? function wordGen() { var wordList = ["Array", "Function", "Boolean", "Null", "Undefined", "Number", "String", "Symbol", "Object"]; var rand = Math.floor(Math.random() * wordList.length); return wordList[rand]; }

22nd Jun 2018, 1:21 AM
Zeppelin Fogarty
Zeppelin Fogarty - avatar
5 Answers
+ 13
Use the onclick atrribute of the <button> tag. One more thing to note is that the function returns a value and not actually print it on the screen..So I used console.log() function to show the value. Feel free to ask if you don't understand :) https://code.sololearn.com/WF8vBc4YamT4/?ref=app
22nd Jun 2018, 1:30 AM
Nikhil - avatar
22nd Jun 2018, 1:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Thank you, I am in my second week of a full stack coding bootcamp. Everything is coming at me really fast.
22nd Jun 2018, 2:13 AM
Zeppelin Fogarty
Zeppelin Fogarty - avatar
+ 2
May be this code can help you👍😉🌀💫💫💫 https://code.sololearn.com/WpR8qrTKmfRi/?ref=app
22nd Jun 2018, 2:28 PM
Shahzod Shark
Shahzod Shark - avatar
+ 1
If you are attending a Bootcamp I will assume that you are not using Code Playground to create a website. You will need to point to the external javascript file as an example- <script> src="something.js"</script> see links below for more examples and explanations https://www.w3schools.com/js/js_whereto.asp https://www.youtube.com/watch?v=r--dRuIiV7A
22nd Jun 2018, 4:48 PM
ODLNT - avatar