1

This script is running if i dont put it into a function but doesnt work inside function..

Please explain the logic to me

    $(document).ready(function(){
       var active=$('.active');
       var index=$('#pics li').index(active);
       var maxindex=$('#pics li').length-1;

       function makeCircles(){
           for (var i=0;i<=maxindex;i++)
           {
               var $circle=$("<li>",{class:'circle'});
               $('#navCircles').append($circle);
           }
        }   
      });

The code in makeCircles function is not running. however it works fine if i remove the function and place those lines directly below variables... as follows.

$(document).ready(function(){
var active=$('.active');
var index=$('#pics li').index(active);
var maxindex=$('#pics li').length-1;

    for (var i=0;i<=maxindex;i++)
    {
        var $circle=$("<li>",{class:'circle'});
        $('#navCircles').append($circle);
    }   
});

Thanks in advance.

5
  • can you show us how the "non functioning code" looks like? jsFiddle perhaps? Commented Jul 19, 2013 at 10:30
  • 8
    the function makeCircles is not invoked anywhere Commented Jul 19, 2013 at 10:31
  • 1
    you need to execute a function to execute the code inside so add: makeCircles() Commented Jul 19, 2013 at 10:31
  • Maybe because you're not invoking the function? Commented Jul 19, 2013 at 10:31
  • you have not invoked the function Commented Jul 19, 2013 at 10:33

3 Answers 3

2

You forgot to invoke the function...

$(document).ready(function(){
   var active=$('.active');
   var index=$('#pics li').index(active);
   var maxindex=$('#pics li').length-1;

   (function makeCircles(){
       for (var i=0;i<=maxindex;i++)
       {
           var $circle=$("<li>",{class:'circle'});
           $('#navCircles').append($circle);
       }
    })();   
  });
Sign up to request clarification or add additional context in comments.

2 Comments

why is invoking necessary? and is this () the you invoke function...just asking.
Because what you have done is just declared it. The code will only be executed when you call that function.
0

you only defined the function, you also need to execute it:

$(document).ready(function(){
   var active=$('.active');
   var index=$('#pics li').index(active);
   var maxindex=$('#pics li').length-1;

   function makeCircles(){ //define the function
       for (var i=0;i<=maxindex;i++)
       {
           var $circle=$("<li>",{class:'circle'});
           $('#navCircles').append($circle);
       }
    }   
    makeCircles(); //execute the function
  });

1 Comment

ohh!! thanks a lot! i just got confused and forgot to do that.
0
$(document).ready(function(){
   var active=$('.active');
   var index=$('#pics li').index(active);
   var maxindex=$('#pics li').length-1;

   function makeCircles(){ 
       for (var i=0;i<=maxindex;i++)
       {
           var $circle=$("<li>",{class:'circle'});
           $('#navCircles').append($circle);
       }
    }   
    makeCircles(); // call this function
});

Please try this.

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.