Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How to shorten this JQuery Code

I'm new to javascript/jQuery. Is there a way to make this code shorter?

else if (players == 6) {
    $('#box1').addClass("col-md-4");
    $('#box1').removeClass("col-md-6");
    $('#box2').addClass("col-md-4");
    $('#box2').removeClass("col-md-6");
    $('#box3').addClass("col-md-4");
    $('#box3').removeClass("col-md-6");
    $('#box4').addClass("col-md-4");
    $('#box4').removeClass("col-md-6");
    $('#box4').removeClass("col-md-offset-4");
    $('#box5').addClass("col-md-4");
    $('#box5').removeClass("col-md-6");
    $('#box6').addClass("col-md-4");
    $('#box6').removeClass("col-md-6");
    $('#box1').show();
    $('#box2').show();
    $('#box3').show();
    $('#box4').show();
    $('#box5').show();
    $('#box6').show();
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can combine the selectors and apply each method with chaining.

else if (players == 6) {
   $('#box1,#box2,#box3,#box4,#box5,#box6')
        .addClass("col-md-4")
        .removeClass("col-md-6")
        .show();
}

Or use attribute starts with the selector.

else if (players == 6) {
   $('[id^="box"]')
        .addClass("col-md-4")
        .removeClass("col-md-6")
        .show();
}


Or use a common class for elements and select based on that.

else if (players == 6) {
   $('.box')
        .addClass("col-md-4")
        .removeClass("col-md-6")
        .show();
}
about 4 years ago · Santiago Trujillo Report

0

when you could give all your boxes a class like .box you could do this (preferred)

$('.box').addClass("col-md-4").removeClass("col-md-6").show();

or you could do this

$('#box1, #box2, #box3').addClass("col-md-4").removeClass("col-md-6").show();

in stead of:

$('#box1').addClass("col-md-4");
$('#box1').removeClass("col-md-6");
$('#box2').addClass("col-md-4");
$('#box2').removeClass("col-md-6");
$('#box3').addClass("col-md-4");
$('#box3').removeClass("col-md-6");
$('#box4').addClass("col-md-4");
$('#box4').removeClass("col-md-6");
$('#box4').removeClass("col-md-offset-4");
$('#box5').addClass("col-md-4");
$('#box5').removeClass("col-md-6");
$('#box6').addClass("col-md-4");
$('#box6').removeClass("col-md-6");
$('#box1').show();
$('#box2').show();
$('#box3').show();
$('#box4').show();
$('#box5').show();
$('#box6').show();
about 4 years ago · Santiago Trujillo Report

0

You can do the following:

else if (players == 6) {
   $('[id^="box"]').removeClass("col-md-6").addClass("col-md-4").show();
}

or you can use attribute like

else if (players == 6) {
   $('[data-box="true"]').removeClass("col-md-6").addClass("col-md-4").show();
}

All those elements should have data-box="true" attribute.

Also, you could use some dummy class with your slectors and do the following:

else if (players == 6) {
   $('.dummy').removeClass("col-md-6").addClass("col-md-4").show();
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!