Why code fragments that looks similar works in different ways?
<input data-value-max="10">
1. If script gets option
from attr
, it always updates input
with option
value:
$('input').keyup(function(e) {
var $this = $(this);
option = $this.attr('data-value-max');
val = $this.val();
if (val > option){ // ←---
e.preventDefault();
$this.val(option);
}
});
I mean that if I type 3
or 10
, script updates input to 10
.
2. The second variant works just as I expect—it replaces input value only if it is greater then a number in if
statement:
$('input').keyup(function(e) {
var $this = $(this);
option = $this.attr('data-value-max');
val = $this.val();
if (val > 10){ // ←---
e.preventDefault();
$this.val(option);
}
});
So I can't understand why the first variant replaces input value all the time?