0

If i click button i want to call inside function hello.is it possible to call ?

 <!DOCTYPE html>
 <html>
 <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"    type="text/javascript"></script>

  <script>
  function hai(){
      alert("hai fun");
   function hello(){
     alert("hello function");
    }
 }
 </script>
 </head>
 <body>
 <button onclick="hello()">click here</button>
 </body>
 </html>
2
  • 1
    Welcome to SO! It would help you get more relevant answer if you clearly explain what output do you expect here. Also: you are not really using jQuery here, just pure JavaScript. Commented Mar 5, 2014 at 6:03
  • in general "hello()" function is only visible inside "hai()" function, if you still want to call it, you will need to make it visible to the outside, for example, by declaring variable on the same level as "hai()": var hello; function hai(){... hello = function {alert(...);} Commented Mar 5, 2014 at 6:08

3 Answers 3

5

The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function.

function hai(){
      alert("hai fun");
 }

function hello(){
     alert("hello function");
     hai();
    }

<button onclick="hello()">click here</button>

DEMO

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

3 Comments

Seems like OP is trying to call nested function, might worth explaining why it's not possible.
i am asking is it possible to call function within the function ?
@Dileep: You cannot call a function defined inside another from outside that function. The inner function doesn't even exist.
0

set it to a variable:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"    type="text/javascript"></script>
        <script>
            function hai(){
                alert("hai fun");
                whatYouAreLookingFor = function hello()
                {
                    alert("hello function");
                }
                whatYouAreLookingFor();

            }
        </script>
    </head>
    <body>
        <button onclick="hai()">click here</button>
    </body>
</html>

1 Comment

I was kinda hoping variable will be named isItMeYouLookingFor :)
0

Here is a way to call the inner function but it requires modifying the code of the outer function and passing in a variable to grab the inner function.

HTML

<body>
    <button onclick="funcVariable()">click here</button>
</body>

Javascript

var funcVariable;

function hai(funcVar) {
    funcVar = function hello() {
        alert("hello function");
    };

    return funcVar
}

funcVariable = hai(funcVariable);

Pressing the button will now show the alert.

See JSFiddle

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.