3

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();
    });
})
1
  • 7
    You are wrongly concatening string here. Commas should be part of former string. Anyway, you would have better to use $(theMvp.toString()).find('.budTySales, .lySales, .budReceipts, .lyReceipts').hide(); or better just use a specific common class... Commented Jul 19, 2016 at 18:40

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

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.