This works fine, except when then data-* is equal to "0", can anyone explain why.
If I access the attribute directly it works fine, for example $("#Floor0").data("floor") returns 0 as expected.
But if I use the filter-function to find a parent with data-floor, it will not work if the parents data-floor="0".
If I change the data-floor="0" to data-floor="00", or any other value, it will work.
console.log($("#Floor2").data("floor"));
console.log($("#Floor1").data("floor"));
console.log($("#Floor0").data("floor"));
Test('a2');
Test('a1');
Test('a0');
function Test(id){
var FloorId = $("#"+id).parents().filter(function() {
return $(this).data("floor");
}).eq(0);
console.log(FloorId.data("floor"));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div data-floor="0" id="Floor0">
<div>
<label>0</label>
</div>
<div>
<input type="checkbox" id="a0" />
</div>
</div>
<div data-floor="1" id="Floor1">
<div>
<label>1</label>
</div>
<div>
<input type="checkbox" id="a1" />
</div>
</div>
<div data-floor="2" id="Floor2">
<div>
<label>2</label>
</div>
<div>
<input type="checkbox" id="a2" />
</div>
</div>
</div>
<br>
<br>