简体   繁体   中英

How do I call a function inside of another function from java script file in html file?

I want to call the function that is in another function in my HTML file here is my HTML code:

    <input type="radio" name="layerButton" value="UnTiled" id="raster" onclick="if(this.checked){myFunction()}">Raster Layer
    <script type="text/javascript" src="./main.js" ></script>

and here is my javascript code(main.js):

function init(){
function myFunction() {
    alert('hi')
}
}

and error is:

Uncaught ReferenceError: myFunction is not defined

Here is the solution snippet

 function init(){ function myFunction() { alert('hi'); } return myFunction(); }
 <input type="radio" name="layerButton" value="UnTiled" id="raster" onclick="init()">

<input type="radio" name="layerButton" value="UnTiled" id="raster" onclick="myFunction()">Raster Layer
<script type="text/javascript" src="./main.js"></script>

function myFunction() {
  alert('hi')
}

This is my solution: https://jsfiddle.net/

To add some context to the other answer, you are not able to run conditional statements using onClick, only functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM