0

Let's say I have a for loop dynamically generating Jquery functions - like so:

 function addJqueryFunctions(divListJSON) {
    for(var i in divListJSON)
    {
        var id = divListJSON[i].id;

        $('#' + id + ').magicfunction();
    }
}

How do I make it so that the Jquery functions generated in the for loop make it to the page and are run?

1
  • 1
    You're not generating functions, you're just calling the same function repeatedly. Commented Aug 27, 2014 at 21:37

1 Answer 1

1

To select an element by ID, you need to prefix it with #

$('#' + id).magicFunction();

Notice that the variable and + operator have to be outside the quotes.

Sign up to request clarification or add additional context in comments.

4 Comments

Left the prefix out in the original post, but will functions work if generated in a loop? I.e. $('#id').draggable
You're not "generating" anything, you're just calling a function. And each time, you're calling it with a different context parameter.
Your modified question has an extra quote before ). If you have that in your actual code it will report a syntax error.
There are some issues with generating closures in for loops. See stackoverflow.com/questions/1451009/…. But you don't have anything like that in your question.

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.