Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was wondering if anyone would mind looking over my JSFiddle and letting me know if this is best practice for the operation.

I am looking at it and it works but it seems very verbose. The code basically allows a top level set of checkboxes, it you think of it in a table structure it would be the first row and only checkboxes in the column that has the active checkbox can be selected.

I am relying very heavily on the naming convention of classes and ids together with the jQuery $(this).

Basically the classes of the bottom level checkboxes have to be the same name as the top level ids. The bottom level checkboxes are also disabled using a class name that all the bottom level checkboxs have.

I am kinda new to jQuery and I am not sure this is the best way to do it.

$(function () {
    $(".service-slider").on("click", function () {

    // Disable all bottom level slides
    $(".slide").attr("disabled", false);

   // Uncheck all previous checked checkboxes
   $(".slide").prop('checked', false);

   // Disable top level checkboxs
   $(".service-slider").prop('checked', false);

   // Enable the selected checkbox to true
   $(this).prop('checked', true);

   //Disable bottom level slides
   $(".slide").attr("disabled", true);

   // Get selected id to enable all bottom level slides based on this
   var tron = "." + $(this).attr("id");

   // Get the var and enable only those checkboxes
   $(tron).attr("disabled", false);

   });
 });
share|improve this question
    
There is something that is bugging me when people are using jQuery to handle states or application flow. The logic code becomes dependant of the view. Though, I think it's as good as jQuery allows you to do. You could use functions with meaningful names so that the comments would be useless. If you plan on creating something bigger, you should consider using a framework over a dom manipulation library. I suggest AngularJS or Backbone. –  jackdbernier Feb 6 at 17:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.