DummyDummy DummyDummy - 4 months ago 9
HTML Question

How to put a color in html textbox with a condition?

Hello guys how to change a color of a textbox in cshtml?. I have this code

$("#BUTTON").click(function (event) {
var urlPA = URL;
var crty = $("#Courtesy").val();
var fname = $("#Familyname").val();
var gname = $("#GivenName").val();

if (crty != 0 && fname != 0 && gname != 0) {
$.ajax({
type: "GET",
url: urlPA,

data: 'DATA',
success: function (response) {
alert(response);
/// when success the color of the textbox return to normal
},
datatype: "text"
});
} else { alert("Please fill up the required fields.");
////// when error the textbox background makes red

}
});


How to do it?. Thanks.

Answer

You can use jQuery's css function to set an inline style:

if (crty != 0 && fname != 0 && gname != 0) {
    $.ajax({
        type: "GET",
        url: urlPA,

        data: 'DATA',
        success: function (response) {
            alert(response);
            /// when success the color of the textbox return to normal
            $('#Courtesy').css('background-color', 'white');
            // add other fields
        },
        datatype: "text"
    });
} else { alert("Please fill up the required fields.");
    ////// when error the textbox background makes red
    $('#Courtesy').css('background-color', 'red');
    // add other fields
}