0

I am have created a multi select filter. For each of the options selected the new div element will be created with it's id is the value of checkbox selected. Till here it's working fine. But now I want to remove those div who's options(checkboxes) are un selected. I tried the below,

 if(!($(this).is(":checked"))){
      alert('is un-checked: ' + $(this).val());
   }

but it's not working. Giving value of null. Can anyone please suggest me how can I achieve this?

CODE:

                     if (window.XMLHttpRequest)
                    {
                        areq = new XMLHttpRequest();
                    } else
                    {
                        areq = new ActiveXObject("Microsoft.XMLHTTP");
                    }

                    areq.onreadystatechange = function () {
                     if ((areq.readyState == 4) && (areq.status == 200)) {
                           document.getElementById("details7").innerHTML= areq.responseText;
                      var c=areq.responseText;
                              $('.matrb').SumoSelect({
                                    triggerChangeCombined: false,
                                    okCancelInMulti: true,

                               });
                        $('.matrb').on('change', function() { 

                        if ($('option:selected', this).is(':checked')) {

      alert('is checked: ' + $(this).val()); 
     am=$(this).val(); 
     nm=$(this).find('option:selected').attr("name");
      am = am.toString().match(/\w+$/)[0];           
console.log("am is:"+c); 

   } 
   else if(!($(this).is(":checked"))){
      alert('is un-checked: ' + $(this).val());
   }

                             if (window.XMLHttpRequest)
                    {
                        breq = new XMLHttpRequest();
                    } else
                    {
                        breq = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                             breq.onreadystatechange = function () {
                     if ((breq.readyState == 4) && (breq.status == 200)) {
                         if(!( document.getElementById(am))){ 
                             var namee=document.createElement('p');
                          var newDiv=document.createElement('div');
                          newDiv.setAttribute('id', am);
                            newDiv.setAttribute("style","display:inline;");
                             namee.setAttribute("style","display:inline;");
                          var htm=breq.responseText;
                          newDiv.innerHTML=htm;
                          namee.innerHTML=nm;
                          console.log(htm);
                          console.log(newDiv);
                          document.getElementById("details8").appendChild(namee);
                             document.getElementById("details8").appendChild(newDiv);
                         }
techhunter
  • 683
  • 1
  • 12
  • 27

6 Answers6

1

Something like this perhaps:

 $('#checkboxes_container_id').find('input[type="checkbox"]')‌​.on('change', function (e) {
    $('#checkboxes_container_id').find('input[type="checkbox"]').each(function(index, element) {
        if (!$(this).is(':checked')) {
            alert('is un-checked: ' + $(this).val());
            return false; // in case you only want ONE alert
        }
    });
}
Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28
  • Thanks for response. I want only the value of currently unchecked checkbox. – techhunter Dec 05 '16 at 08:55
  • Well yea, this should get you the values of all unchecked checkboxes. If you simply want the 1st one you can simply terminate the loop iteration on the 1st condition hit. – Klemen Tusar Dec 05 '16 at 08:59
  • Not for firs one. By default all the checkboxes are unchecked. user can check and uncheck any of them. – techhunter Dec 05 '16 at 09:04
  • 1
    Well then bind this code to a trigger like: `$('#checkboxes_container_id').find('input[type="checkbox"]').on('change', function (e) { do_stuff(); })` – Klemen Tusar Dec 05 '16 at 09:08
1
var uncheckedValues = $("select#id").find('option').not(':selected');
var uncheckedArray = uncheckedValues.map(function () { return this.value;}).get();
console.log(uncheckedArray);
chaixxiv
  • 421
  • 4
  • 6
  • While this answer may solve the problem, please try not to copy/paste code. Instead explain what the code does and why it is relevant to the OP. – Nahuel Ianni Dec 05 '16 at 09:42
1
$('input[type=checkbox]').not(':checked')

$('#test').on('click', function() {
  console.log($('input[type=checkbox]').not(':checked').length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />

<button id="test">Test</button>

$('input[type=checkbox]').on('change', function() {
  if(!this.checked){
    console.log('unchecked checkbox');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />

<button id="test">Test</button>
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
1

You can use "not" for the selecion

$('[attribute="value"]:not(":checked")').each(function() {
  alert($(this).val());
});

Check this https://jsfiddle.net/wrajesh/3ga508x3/3/

lost_in_magento
  • 703
  • 8
  • 22
1

First of all, you need to bind change event for each checkbox so that whenever any checkbox is clicked (current one in your case) you can check if it is checked or unchecked.

$('#parent_container_selector').find('input[type="checkbox"]').each(function(index, element) {

       $(this).on('change', function(){
             //this is your current event! Grab it and do your logic here.
            if($(this).is(':checked') == false)
            {
               //delete your required elements..
            }
       });
});
ScanQR
  • 3,740
  • 1
  • 13
  • 30
  • tried. But if($(this).is(':checked') == false){ alert( $(this).val()); } is null. – techhunter Dec 05 '16 at 10:12
  • @safoorasafu then it means your checkboxes dont have any values defined. because you do get into if block on checkbox change event. – ScanQR Dec 05 '16 at 10:19
  • Thanks for above answer. But it has value.and passing the value as id for next div i have generated new content. but now i want to delete the div when checkbox having corresponding value is un selected. for this i need the value of checkbox. – techhunter Dec 05 '16 at 12:04
0

Finally I found the solution. below is the code for that,

$('#myselect').on('change', function() {
  var $sel = $(this),
    val = $(this).val(),
    $opts = $sel.children(),
    prevUnselected = $sel.data('unselected');
  // create array of currently unselected 
  var currUnselected = $opts.not(':selected').map(function() {
    return this.value
  }).get();
  // see if previous data stored
  if (prevUnselected) {
      // create array of removed values        
      var unselected = currUnselected.reduce(function(a, curr) {
        if ($.inArray(curr, prevUnselected) == -1) {
          a.push(curr)
        }
        return a
      }, []);
      // "unselected" is an array
      if(unselected.length){
        alert('Unselected is ' + unselected.join(', '));  
      }

  }
  $sel.data('unselected', currUnselected)
}).change();

posting this because it may help someone.

techhunter
  • 683
  • 1
  • 12
  • 27