1

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

2 Answers 2

2

To convert your array to a jQuery selector:

Change :

$(ids)

to

$(ids.join()) // returns $('#biography, #videogallery, ..., #resume ')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it works fine now, Thanks again for your help.
1

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);
});

1 Comment

try logging this on a string array using each. It isn't what you expect

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.