0

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.

3
  • 1
    Define setData outside the click callback block Commented Feb 2, 2018 at 5:48
  • var value=setData(); function addSubjectTeacher(){console.log(value)} Commented Feb 2, 2018 at 5:50
  • 1
    This 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. Commented Feb 2, 2018 at 5:50

1 Answer 1

1

You can just define your var current outside [globally] and get its value as below.

Assumption - You will be calling addSubjectTeacher function after click event of td-subject-button

var current = "";
$(document).on('click','.td-subject-button',function(){ 
   current= $(this).parent('td');     
});

function  addSubjectTeacher(){
   var value=current;
   console.log(value);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Anytime bro.. Happy coding.. :)

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.