开发者

How to change CSS attributes of a label, dynamically?

I ve a label in my cshtml page like : @Html.LabelFor(model => model.VATNumber)*

I want to change the color of the label based on some condition at runtime. But the below code in my JS is not working. Any syntax / logical error could you see?

    if (!enteredTaxRegNo.match(validpattern)) {
        $('#VATNumber').css('color', "Red");  //TODO: As开发者_高级运维sign the proper label id 
        $('#VATNumber').attr("title", "Please select proper Tax Reg No.");           
    }
    else {
        $('#VATNumber').css('color', "Black");  //TODO: Assign the proper label id 
        $('#VATNumber').attr("title", "");
     }


Works here. This leads me to suspect that you are setting the css before the label is actually created like this which doesn't work. You must make sure your label is appended to your DOM before you try to set it's attribute. In your case, it might be simply waiting for $(document).ready() or you need to have your code execute after your #VATNumber is created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜