Gautam
Gautam

Reputation: 3384

Disable last option to be checked in Jquery Multiselect

I am using jquery multiselect in my project,when user clicks on checkall then how can i stop this function to check last option in jquery multiselect?

like if i have 4 option in my jquery multiselect and if user clicks on check all i want intial three option to be checked but last option is unchecked. any help would be much appreciated

Upvotes: 2

Views: 3685

Answers (2)

Tats_innit
Tats_innit

Reputation: 34117

Here you go updated solution here for plugin as well normal multiple checkbox with demo!

Solution for multiselect Checkbox special condition to uncheck last checkbox on checkall

Working MultiSelect jsfiddle for specific case regarding @Gautam's multiselect plugin: http://jsfiddle.net/fG6PT/82/

HTML:

<select multiple="multiple" class='multi'>
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="baz">baz</option>
<option value="toy">toy</option>
</select>

JQuery

$('.multi').multiselect({

    // Uncheck the last checkbox when chaeckall happens.
    checkAll: function () {

       if ($(this).is(":last") == true){

          var arr = $(this).val().toString().split(',');
           // Work on the array length alert('===>' + arr.length);
           var l = arr.length;
           for(i=(l-1);i<l;i++){
               // so whatever the lenght is aim for the last element
                 $('input:checkbox[value="' + arr[i] + '"]').attr('checked', false);
           }
        }
    }

});

Solution below is for normal Chckbox

Working example here: http://jsfiddle.net/Ev7FX/5/

http://jsfiddle.net/Ev7FX/4/

Html

<input type="checkbox" name="check[]" value="1" />
<input type="checkbox" name="check[]" value="2" />
<input type="checkbox" name="check[]" value="3" />
<input type="checkbox" name="check[]" value="4" />
<a href="#" title="" id="check">check all</a> <a href="#" title="" id="uncheck">uncheck all</a>​

JQuery Code

var all_checkboxes = jQuery(':checkbox');

jQuery('#check').on('click', function(event){
    event.preventDefault();
       all_checkboxes.not(':last').prop('checked', true);
});

jQuery('#uncheck').on('click', function(event){
    event.preventDefault();
    all_checkboxes.prop('checked', false);
});​

This will help, cheers!

Upvotes: 2

Ohgodwhy
Ohgodwhy

Reputation: 50798

Capture the checkboxes, iterate through them, perform(or in your case, don't perform) a unique action for the last element found.

var ourCheckBoxes = $(":checkBoxes");
$.each(ourCheckBoxes, function(index,value){
  if(index = ourCheckBoxes.length -1){
    //this is our last element broski
  }
});

Upvotes: 0

Related Questions