Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change input border-color, with jQuery, depending on its value?

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");
}

});
like image 247
Lasha Avatar asked May 18 '13 18:05

Lasha


People also ask

How do you add color to a border?

Adding BordersOpen the Borders and Shading dialog box. Select the Borders tab. From the Style scroll box, select the desired border style. From the Color pull-down list, select the desired border color.


2 Answers

Just trigger the event:

$("input").change(function() {
    // ...
}).trigger("change");

DEMO: http://jsfiddle.net/9B5SX/1/

like image 144
VisioN Avatar answered Sep 21 '22 03:09

VisioN


I'd suggest:

$('input').on('change', function(){
    var v = parseInt(this.value, 10);
    $(this).css('border', function(){
        if (v < 7) {
            return '5px solid #f00';
        }
        else if (v == 7) {
            return '5px solid #0f0';
        }
        else if (v > 7) {
            return '5px solid #f90';
        }
        else {
            return '5px solid #000';
        }
    });
}).change();

JS Fiddle demo.

Though honestly, given that every situation seems to require a border-width of 5px and border-style of solid, I'd set those parts in the CSS:

input {
    border-width: 5px;
    border-style: solid;
}

And simply update the border-color in the jQuery:

$('input').on('change', function(){
    var v = parseInt(this.value, 10);
    $(this).css('border-color', function(){
        if (v < 7) {
            return '#f00';
        }
        else if (v == 7) {
            return '#0f0';
        }
        else if (v > 7) {
            return '#f90';
        }
        else {
            return '#000';
        }
    });
}).change();

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:

$('input').on('change', function(){
    var v = parseInt(this.value, 10);
    $(this).css('border-color', function(){
        var v = parseInt(this.value,10);
        return isNaN(v) ? '#000' : v < 7 ? '#f00' : v == 7 ? '#0f0' : '#f90';
    });
}).change();

JS Fiddle demo.

References:

  • change().
  • on().
  • parseInt().
like image 29
David Thomas Avatar answered Sep 23 '22 03:09

David Thomas