I have question about jQuery. I have code below which works fine; it changes input
border-color
depending on its value, which is typed and entered. But if in input
's value on page-load it does not change that border-color
. How can I make the border-color
change from the beginning, following page-load?
Thank you :)
<input type="text" class="col" value="">
// When the <input>'s value changes
$("input").change(function() {
// If the value is less than 7, add a red border
if ($(this).val() < 7) {
$(this).css("border", "5px solid red");
}
// Else if the value is equal to 7, add a green border
else if ($(this).val() == 7) {
$(this).css("border", "5px solid green");
}
// Else if the value is greater than 7, add an orange border
else if ($(this).val() > 7) {
$(this).css("border", "5px solid orange");
}
// Else if the value is anything else, add a black border
else {
$(this).css("border", "5px solid black");
}
});
I'd suggest:
JS Fiddle demo.
Though honestly, given that every situation seems to require a
border-width
of5px
andborder-style
ofsolid
, I'd set those parts in the CSS:And simply update the
border-color
in the jQuery:JS Fiddle demo.
And, finally, just because I sometimes can't help myself (and while this approach can be taken, it's not one I can honestly recommend...), with added conditional operators:
JS Fiddle demo.
References:
change()
.on()
.parseInt()
.Just trigger the event:
DEMO: http://jsfiddle.net/9B5SX/1/
Same issue here. On my case, the input was a table column and it was displayed for every table row. With the next code I managed to change only one input color, not all when I changed the value for one.
Hope it helps