I want to call a inner function from another function.I have code like
$(document).on('click','.td-subject-button',function(){
var current= $(this).parent('td');
function setData(){
return current;
}
});
function addSubjectTeacher(){
var value=setData();
console.log(value);
}
In the console i am getting the result as 'setData is not defined'. because the setData function is a nested function of a another function. so, how can i invoke setData function.
Tushar Vaghela
1,2431 gold badge17 silver badges25 bronze badges
asked Feb 2, 2018 at 5:45
noushad mohammed
3731 gold badge4 silver badges21 bronze badges
-
1Define setData outside the click callback blockcdoshi– cdoshi2018年02月02日 05:48:20 +00:00Commented Feb 2, 2018 at 5:48
-
var value=setData(); function addSubjectTeacher(){console.log(value)}noushad mohammed– noushad mohammed2018年02月02日 05:50:25 +00:00Commented Feb 2, 2018 at 5:50
-
1This smells like an XY problem. Please explain what your actual problem is and don't get too attached to this attempted solution of calling a scoped function from the global scope.Patrick Roberts– Patrick Roberts2018年02月02日 05:50:30 +00:00Commented Feb 2, 2018 at 5:50
1 Answer 1
You can just define your var current outside [globally] and get its value as below.
Assumption - You will be calling
addSubjectTeacherfunction afterclick eventoftd-subject-button
var current = "";
$(document).on('click','.td-subject-button',function(){
current= $(this).parent('td');
});
function addSubjectTeacher(){
var value=current;
console.log(value);
}
answered Feb 2, 2018 at 5:52
Guruprasad J Rao
29.7k16 gold badges113 silver badges212 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Guruprasad J Rao
Anytime bro.. Happy coding.. :)
lang-js