views:

41

answers:

2

I'm using jQuery Selectbox plugin which has this function:

function setupContainer(options) {
    var container = document.createElement("div");
    $container = $(container);
    $container.attr('id', elm_id+'_container');
    $container.addClass(options.containerClass);

    return $container;
}

... it applies the same ID name to all dropdowns, how do I modify the above code so each id is numbered (starting from 1) and unique?

View the full script here.

Thanks!

A: 

i'm presuming elm_id is a number and it's a global variable.If so the code would look like this:

function setupContainer(options) {
var container = document.createElement("div");
$container = $(container);
elm_id++;
$container.attr('id', elm_id+'_container');
$container.addClass(options.containerClass);

return $container;

}

adivasile
Nope, its not a number. View the full script here: http://pastebin.com/d3d839161
Nimbuz
A: 

You could store elm_id as a property of your function like this

function setupContainer(options) {
    var container = document.createElement("div");
    $container = $(container);

    var elm_id = setupContainer.uid++;

    $container.attr('id', elm_id+'_container');
    $container.addClass(options.containerClass);

    return $container;
}
setupContainer.uid = 1;

This way you don't rely on another global variable.

meouw