There seems to be some stripping issue with jQuery when retrieving integer values from input values.
My code:
// Control the rating input field to only accept numeric values
// -else set the values back to 0
// For the highest value:
$('.dsListItem .firstItem').keyup(function(){
if (isNumber($(this).val()) && $(this).val() < $('.dsListItem .lastItem').val()) {
console.log('FIRST: ' + $(this).val() + ' >>> ' + $('.dsListItem .lastItem').val());
$(this).val($('.dsListItem .lastItem').val());
}
});
// For the lowest value:
$('.dsListItem .lastItem').keyup(function(){
if (isNumber($(this).val()) && $(this).val() > $('.dsListItem .firstItem').val()) {
console.log('LAST: ' + $(this).val() + ' >>> ' + $('.dsListItem .firstItem').val());
$(this).val($('.dsListItem .firstItem').val());
}
});
Safari Inspector tells me this:
LAST: 11 >>> 10
LAST: 2 >>> 10
LAST: 1 >>> 10
Only 0 and 1 are valid. If 10 is validated as “1”, it should not pass 1 > 1 either, but yet it does.
I don’t get it why the console.log() print shows the correct integers but when I calculate with it the 10 get’s converted into a 1.
I tried casting the number with ($(this).val() * 1), yet that didn’t change anything, and anyway; the value is already checked to be a valid numeric value.
If you are sure that you are using numerical values then you have to cast them with a parseInt, so you’ll be sure that Javascript too will be talking with numbers, and not comparing numbers to strings!
I think casting with parseInt is stronger than casting by a *1 instruction…