Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set border color of a texbox using jquery

How to set a default border color of a control using jquery.

       if (_userName.val().trim() == "") {
            errMsg += "\nUserName is a mandatory field.";
            _userName.css('border-color', 'red');
        }
        else {
            _userName.css('border-color', 'red');//Set border-color as loaded 
//when page was loaded
        }

How to Set border-color as loaded when page was loaded.

like image 233
Shantanu Gupta Avatar asked Jul 14 '10 10:07

Shantanu Gupta


2 Answers

Get the border color at page load and store in a variable:

$(function(){
  var color = _userName.css('border-color');
});

And then you can use it later:

 if (_userName.val().trim() == "") {
        errMsg += "\nUserName is a mandatory field.";
        _userName.css('border-color', color);
    }
    else {
        _userName.css('border-color', color);
    }

Also make sure that there is a border at least eg border:1px solid #colorcode

like image 166
Sarfraz Avatar answered Nov 08 '22 13:11

Sarfraz


I would suggest creating a new style class called error and applying it on the textbox when the field contains error. Code snippet:

CSS: .error{border-color:#F00;}

        if (_userName.val().trim() == "") {
            errMsg += "\nUserName is a mandatory field.";
            $("#textboxid").addClass("error");
        }
        else {
            _userName.css('border-color', 'red');//Set border-color as loaded 
            $("#textboxid").removeClass("error");
        }

Advantage: If the field does not have any error, we can just remove the error class and the textbox look and feel will return to the original style. No need to track the original border color explicitly. And the style rule is re-usable too! ;-)

like image 41
Veera Avatar answered Nov 08 '22 12:11

Veera