0

So...I'm trying to shrink down some JS code a bit, and I have things like...

$('#element-A'+idx).hide();
$('#element-R'+idx).hide();
$('#element-Z'+idx).hide();

Is there a reliable way I can smash these down into one statement without taking a performance hit?

I tried:

$('#element-A'+idx,'#element-R'+idx,'#element-Z'+idx).hide();

...but that just gave me an error, which actually is what I thought would happen.

I also tried:

$(['#element-A'+idx,'#element-R'+idx,'#element-Z'+idx]).hide();

...but I also got an error from that.

The only other possibility I can think of is:

$('#element-A'+idx).add('#element-R'+idx).add('#element-Z'+idx).hide();

But doesn't that create a new object, in effect taking up more memory??

What should I do?? Or am I better off just leaving them as separate commands?

2 Answers 2

1

You need to pass a single selector string:

$('#element-A'+idx + ', #element-R'+idx + ', #element-Z'+idx).hide();

Or, better yet, add a common class to all of the elements, and just use that instead.

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

1 Comment

The comma inside the quotes fixed it. Thank you!
1

Your commas need to be inside the quotes:

$('#element-A'+idx,'#element-R'+idx,'#element-Z'+idx).hide();

should be

$('#element-A' + idx + ', #element-R' + idx + ', #element-Z' + idx).hide();

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.