Nick
Nick

Reputation: 14283

Create an array of IDs with jquery

i have a little problem here.

I've created an array where i want to put my ids, but when i use this bit of code, the result is not what i was expecting and i don't know why:

var ids = ['#biography', '#videogallery', '#pubblications', '#contacts', '#resume'];
console.debug(ids);
var currentElem = "";

$(window).load(function () {
    var vWidth = $(window).width();
    var vHeight = $(window).height();
    $(ids).each(function() {
        var currentElem = $(this);
        console.debug(currentElem)
        $(currentElem).css('width', vWidth).css('height', vHeight);
    })
});

basically what i want is to set up the height and width only for specific ids, but the result of the each is something like this:

"#", "b", "i", "o", "g", "r", "a", "p", "h", "y", jquery: "2.1.3", constructor: function, selector: "", toArray: function, get: function…]0: "#"1: "b"2: "i"3: "o"4: "g"5: "r"6: "a"7: "p"8: "h"9: "y"length: 10__proto__: te[0]

why? and how can i solve this? thank in advance

Upvotes: 1

Views: 619

Answers (2)

Satpal
Satpal

Reputation: 133403

Since ids is an array, You can use jQuery.each()

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays.

Use

$.each(ids, function( index, value ) {
    var currentElem = $(value); 
    console.debug(currentElem)
    $(currentElem).css('width', vWidth).css('height', vHeight);
});

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

To convert your array to a jQuery selector:

Change :

$(ids)

to

$(ids.join()) // returns $('#biography, #videogallery, ..., #resume ')

Upvotes: 2

Related Questions