sm1l3y sm1l3y - 4 months ago 8
jQuery Question

issue using .hide() when passing a variable from an array

I have the following code which works fine:

$(function mvp() {
var theMvp = ['#mvpWtd', '#mvpStd'];
$.each(theMvp, function (index, value) {
$(value + ' .budTySales').hide();
$(value + ' .lySales').hide();
$(value + ' .budReceipts').hide();
$(value + ' .lyReceipts').hide();
});
})


According to jquery documentation I should be able to pass multiple elements in followed by a comma instead of doing it line by line (less code too!). I tried changing my code to the below but it fails...

$(function mvp() {
var theMvp = ['#mvpWtd', '#mvpStd'];
$.each(theMvp, function (index, value) {
$(value + ' .budTySales',value + ' .lySales',value + ' .budReceipts',value + ' .lyReceipts').hide();
});
})

Answer

This line:

$(value + ' .budTySales',value + ' .lySales',value + ' .budReceipts',value + ' .lyReceipts').hide();

should be:

$(value + ' .budTySales,' + value + ' .lySales,' + value + ' .budReceipts,' + value + ' .lyReceipts').hide();

Notice that the commas are inside the quotes. This is because jQuery expects a single parameter to be passed into it, not multiple, which is what you were doing.

Comments