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?
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>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments