I am having some trouble with an if statement. I want to set num to 0 of NaN:
$('input').keyup(function() { var tal = $(this).val(); var num = $(this).data('boks'); if(isNaN(tal)) { var tal = 0; } });
The isNaN() method returns true if a value is NaN.
isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.
You can use exclamation mark ! to check if it is null. The above code means that if the $('#person_data[document_type]') has no value (if the value is null).
Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With