Reputation: 1642
Code first:
<style type="text/css">
.label1 {
font-weight: bold;
color: #006699;
}
</style>
<script type="text/javascript">
function test(k) {
document.getElementById(k).style = label1; <--this does not work
}
</script>
I am wondering if it is possible to set the style of the element (which is a label defined in code-behind file (Default.aspx.cs)) using a .CSSclass?
Understand that I can apply .style.color
to set color or etc, but would like to see if above is possible first.
The function test(k) is called from code-behind. Tested to be working.
Upvotes: 1
Views: 451
Reputation: 1074355
document.getElementById(k).style = label1; <--this does not work
There's no reason it would, you don't have a JavaScript variable called label1
, and you don't apply classes by assigning to .style
(though I can see why you'd think of doing it).
Your CSS defines a class. To give the element that class, on modern browsers use classList
:
document.getElementById(k).classList.add("label1");
For older browsers like IE9 and earlier, use className
, which is a space-separated list of classes (like the HTML class
attribute):
document.getElementById(k).className += " label1";
The function test(k) is called from code-behind.
Just make sure k
is the client-side ID of the element. (I think the property for the client-side ID on the server control is creatively named ClientID
, but ASP.Net isn't my thing, apologies if that's the wrong tech.)
Upvotes: 1