vellai durai vellai durai - 4 months ago 11
jQuery Question

Remove null values from list and created using checkbox in jquery

var html = '';
$('.bob').each(function() {
var item = $(this).data('xyz');
if($("input[data-var='"+item+"']").length == 0)
$('.checkboxes').append('<label><input class="testclss" data-var="' + item + '" type="checkbox" />' + item + '</label>');
});


I need to remove the null values and none values from the list

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="bob" data-xyz="fish"></div>
<div class="bob" data-xyz="dog"></div>
<div class="bob" data-xyz="fish"></div>
<div class="bob" data-xyz=""></div>
<div class="bob" data-xyz="fish"></div>
<div class="bob" data-xyz="fish"></div>
<div class="bob" data-xyz=""></div>
<div class="bob" data-xyz="none"></div>

<div class="checkboxes"></div>


Everything just works fine but i need to remove none and "" values from the list

Answer

You can filter in the selector like this :

var html = '';
$('.bob[data-xyz!=""][data-xyz!="none"]').each(function() { 
    var item = $(this).data('xyz');
    if($("input[data-var='"+item+"']").length == 0)  
        $('.checkboxes').append('<label><input class="testclss"  data-var="' + item + '" type="checkbox" />' + item + '</label>');
});
Comments