Sagotharan
Sagotharan

Reputation: 2626

Set css class in asp.net textbox control via javascript

I Have Validate my form with javascript. I want to set ErrorControl css code. But I can't able to get the output.

<style type="text/css">
    .ErrorControl
    {
        background-color: #FBE3E4;
        border: solid 1px Red;
    }
</style>

<script type="text/javascript">
    $(document).ready(function () {
        //HighlightControlToValidate();
        $("#Button1").click(function () {
            if (typeof (Page_Validators) != "undefined") {
                for (var i = 0; i < Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid) {
                        $('#' + Page_Validators[i].controltovalidate).className = "ErrorControl";
                    }
                    else {
                        $('#' + Page_Validators[i].controltovalidate).css("border", "solid 1px White");
                    }
                }
            }
        });
    });
 </script>

Upvotes: 0

Views: 1806

Answers (2)

Starscream1984
Starscream1984

Reputation: 3062

Try changing

$('#' + Page_Validators[i].controltovalidate).className = "ErrorControl";

to

$('#' + Page_Validators[i].controltovalidate).addClass("ErrorControl");

Upvotes: 1

Dheeraj
Dheeraj

Reputation: 318

Try this

<script type="text/javascript">
$(document).ready(function () {
    //HighlightControlToValidate();
    $("#Button1").click(function () {
        if (typeof (Page_Validators) != "undefined") {
            for (var i = 0; i < Page_Validators.length; i++) {
                if (!Page_Validators[i].isvalid) {
                    $('#' + Page_Validators[i].controltovalidate).addClass( "ErrorControl");
                }
                else {
                    $('#' + Page_Validators[i].controltovalidate).css("border", "solid 1px White");
                }
            }
        }
    });
});

Upvotes: 1

Related Questions