jQuery If value is NaN
Solution 1:
You have to assign the value back to $(this)
:
$('input').keyup(function() {
var tal = $(this).val();
var num = $(this).data('boks');
if(isNaN(tal)) {
var tal = 0;
}
$(this).data('boks', tal);
});
nicely written:
$('input').keyup(function() {
var eThis = $(this);
var eVal = (isNaN(eThis.val())) ? 0 : eThis.val();
eThis.data('boks', eVal);
});
Solution 2:
If you want replace the NaN with 0, just write this:
var tal = parseInt($(this).val()) || 0;