Kurkula
Kurkula

Reputation: 6762

Jquery using multiple selectors to show all at a time

I have jquery code to show 3 items at a time

$('.a').show();
$('.b').show();
$('.c').show();

I can also achieve the same with

$('.a, .b, .c').show();

What is main difference between both codes? Is it only the code reduction or will there be any performance related changes with 2nd code?

Upvotes: 0

Views: 47

Answers (2)

Alcadur
Alcadur

Reputation: 685

In more details, when You call

$('.a').show();
$('.b').show();
$('.c').show();

It works like that:

  • find all elements with class a and call method show
  • find all elements with class b and call method show
  • find all elements with class c and call method show

so You create 3 arrays.

When you this:

$('.a, .b, .c').show();

It means: find all elements with classes a, b, c and call method show, so you create just one array.

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

By the way you can use a common class to achieve that:

$('.common_class').show();

But with your question:

With first idea of code would perform one by one but with second idea of code will do once by searching the classes.

Upvotes: 1

Related Questions