0

I'm just trying to figure out how to create an index inside a function so I can keep track of the items its generating and I'm not sure how to do this seems like I should know this..

addLocation(options,location)

   funciton addLocation(options,location){
       $('#list').append('<li class="location"><div>'+location.label+'</div>'+
        '<input type="hidden" name="location['+x+'][lat]" value="'+location.lat+'" />'+
        '<input type="hidden" name="location['+x+'][lon]" value="'+location.lon+'" />'+
        '<input type="hidden" name="location['+x+'][loc]" value="'+location.loc+'" />'+
        '<input type="hidden" name="location['+x+'][label]" value="'+location.label+'" />'+
    '</li>');
   }

now normally you have a forloop to help you keep track of things but int this instance I'm not appending items in a loop and I keep getting an error saying x in undefined

appreciate any help you can give

thanks Mike

5
  • 1
    What value(s) should x be in this example? Commented May 3, 2010 at 19:03
  • @Zack values should be an integer @vicatcu i haven't defined x because I'm not sure how to with out making x 0 all the time Commented May 3, 2010 at 19:08
  • @mcgrailm, do you mean that each time the function is called, x should be incremented? Commented May 3, 2010 at 19:09
  • @mcgrailm, clear enough now, I posted an answer... Commented May 3, 2010 at 19:15
  • @CMS not really I'm not sure how to call the function now Commented May 3, 2010 at 19:33

3 Answers 3

3

Since what you want is that each time the function is called, x should be incremented, you can store x in a closure, for example:

var addLocation = (function (){
  var x = 0; // store x
  return function (options,location) {
    x++; // increment x
    $('#list').append('<li class="location"><div>'+location.label+'</div>'+
    '<input type="hidden" name="location['+x+'][lat]" value="'+location.lat+'" />'+
    '<input type="hidden" name="location['+x+'][lon]" value="'+location.lon+'" />'+
    '<input type="hidden" name="location['+x+'][loc]" value="'+location.loc+'" />'+
    '<input type="hidden" name="location['+x+'][label]" value="'+location.label+'" />'+
    '</li>');
  };
})();
Sign up to request clarification or add additional context in comments.

2 Comments

so then I call the the function by variable ? how are the parameters gonna make it into the function sorry never seen this done before
just call addLocation(options, location). The anonymous function with no parameters is there for the closure, the inner function that takes two parameters is what gets assigned to addLocation.
0

Declare x since in your above code it's not defined.

var x = 0;

1 Comment

Well you will have to define it as something I just put 0 because I don't know what value you are trying to derive. What are you basing the index off of, looks like zincorp has the right idea of what to base the index off of.
0
var x = $("#list>li").length + 1;

2 Comments

If the items get removed, this should still account for that as long as you are removing <li>'s
ok so lets say i have 4 li's x should be up to 3 then I add a fifth x goes to 4 i remove li3 and my x remains at 4

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.