Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    });
})
like image 654
sm1l3y Avatar asked Sep 29 '26 05:09

sm1l3y


2 Answers

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.

like image 194
imtheman Avatar answered Sep 30 '26 18:09

imtheman


As stated in the comments, comma should be part of the string.

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

like image 29
larz Avatar answered Sep 30 '26 20:09

larz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!