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.
精彩评论