0

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
3
  • 1
    You should use closure js closure Commented Mar 7, 2021 at 9:02
  • I think Beller's comment is the answer. I hope he/she will post it as an answer as well. Commented Mar 7, 2021 at 9:05
  • yes, I hope too! Commented Mar 7, 2021 at 9:10

3 Answers 3

2

Here is the solution snippet

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

Sign up to request clarification or add additional context in comments.

1 Comment

I found one problem. when open page and before selected radio button, alert is showing
0
<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/

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.