0

A rather trivial question, how do I return or pass a value outside the function?

Code below:

function numberOfDivs(){
 var numberOfElements = $("#div").children().length; //this count how many divs exist
 return numberOfElements;
}

$("#element").click(function(){
  numberOfDivs();   
  console.log(numberOfElements);// here I need to return the number but getting error :(
});

many thanks

6
  • 1
    This is a callback of an async function. You can't return anything directly. Commented Feb 5, 2014 at 11:24
  • @medzi, this is pretty basic stuff, maybe you should pick a tutorial and get more used to the language. Commented Feb 5, 2014 at 11:25
  • 1
    @Danilo, where do you see an async call here? This is a simple matter of storing the value returned by numberOfDivs(). Commented Feb 5, 2014 at 11:26
  • .click method attaches an event listener to node of id element, and therefore is async Commented Feb 5, 2014 at 11:28
  • @Danilo, the caller being async is not a problem, as long as the callee (numberOfDivs()) is not async itself (which it isn't). Commented Feb 5, 2014 at 11:31

5 Answers 5

2
$("#element").click(function(){
  var numberOfElements= numberOfDivs();   
  console.log(numberOfElements);
});
Sign up to request clarification or add additional context in comments.

Comments

2

Try

var numberOfElements= numberOfDivs();   
console.log(numberOfElements);

As the function is to return a value, while we invoke the function we assigned a variable to capture the result

Comments

1
var numberOfElements;    
function numberOfDivs(){
    numberOfElements = $("#div").children().length; //this count how many divs exist
            }

    $("#element").click(function(){
             console.log(numberOfElements);// here I need to return the number but getting error :(
    });

Comments

1

One way is : define numberOfElements in global scope like this :

var numberOfElements;
function numberOfDivs(){
 numberOfElements = $("#div").children().length; //this count how many divs exist
 return numberOfElements;
}

$("#element").click(function(){
  numberOfDivs();   
  console.log(numberOfElements);// here I need to return the number but getting error :(
});

Or another way is : assign the result in one variable and use that

$("#element").click(function(){
      var output = numberOfDivs();   
      console.log(output);// here I need to return the number but getting error :(
    });

Comments

1

Try using Callback functions, consider a scenario where incase your numberofDivs function takes time to return the value, it will not give the appropriate results as Jquery is an asynchronous. See this demo for the use of callback to return the data CALLBACK DEMO

function AppendJson(callback) {
   var numberOfElements = "AppendJson";
   callback(numberOfElements)
}


AppendJson(function (ReturnValue) {
   alert(ReturnValue);
});

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.