rpsep2
rpsep2

Reputation: 3121

trouble setting 'dynamic' variable

i want to set a variables name dynamically, I've been trying:

var iframes = $('iframe').filter(function (index) {
                return index == 0 || $(this).attr("current") == "no";
              })
        if(iframes.length >0){ //if one of the iframes hasnt got current set as current, use it
            var theSuffix = iframes.attr('id').split('_').pop();
            window['thisPreview'+theSuffix] = $fi.prev(".image-preview");
            $hidden.closest(".file-upload-form").find(".variable-hidden").attr('value',theSuffix);
        }

where I want a variable called 'thisPreview' and then a number, which will be the id of the matched div, or the variable 'theSuffix' (variable needs to be global, too).

how can I achieve this?

Upvotes: 0

Views: 26

Answers (1)

Blender
Blender

Reputation: 298582

Don't make variables that have a common prefix and numbers at the end. Just use an object:

var thisPreview = {};

...

thisPreview[theSuffix] = $fi.prev(".image-preview");

Upvotes: 2

Related Questions