1

I have some JavaScript function like below:-

function temp() {
    var rep ="some";
}



function solve() {
  function temp();
  $("#demo").text('solve1:'+rep);
}

function solve1() {
  function temp();
  $("#demo1").text('solve2:'+rep);
}
  
function solve2() {
  function temp();
  $("#demo2").text('solve3:'+rep);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<button onclick="solve()">solve</button><br>
<button onclick="solve1()">solve</button><br>
<button onclick="solve2()">solve</button><br>

My question is how to callback first function temp() into all other three functions. Above script is not working. How do I include one JavaScript function within multiple functions?

1
  • scope of variable rep is inside the function Commented May 18, 2016 at 11:03

1 Answer 1

1

First of all, you need to return the value of the variable rep from your temp() function. Then wherever you call that function the value will be available. Note that you don't have to use the keyword function while calling the function. Just the name of the function will do. See the code comments to understand better.

function temp() {
    var rep = "some";
    return rep;        // Return the value of the 'rep' variable.
}



function solve() {
  var rep = temp();        // Call the 'temp()' function defined above.
  $("#demo").text('solve1:'+rep);
}

function solve1() {
  var rep = temp();
  $("#demo1").text('solve2:'+rep);
}
  
function solve2() {
  var rep = temp();
  $("#demo2").text('solve3:'+rep);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<button onclick="solve()">solve</button><br>
<button onclick="solve1()">solve</button><br>
<button onclick="solve2()">solve</button><br>

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

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.