I need to change a value of a Class Style Property Color from RED to GREEN on click.
JavaScript
x
19
19
1
<style>
2
.c1
3
{
4
color : RED;
5
}
6
7
</style>
8
9
<a id="a1" class="c1" onclick="changeclassstyle()">
10
Hi
11
</a>
12
13
<script>
14
function changeclassstyle()
15
{
16
/* here i need to change the style of class c1 */
17
}
18
</script>
19
I need to change the color of c1 class to GREEN.
Note: I dont want it to be done with “ID” & I dont want to create new Class and change the class name. I want it to happen with same class name.
Advertisement
Answer
JavaScript
1
6
1
function changeclassstyle() {
2
var c = document.getElementsByClassName("c1");
3
for (var i=0; i<c.length; i++) {
4
c[i].style.color = "green";
5
}
6
}
JavaScript
1
3
1
.c1 {
2
color : RED;
3
}
JavaScript
1
3
1
<a id="a1" class="c1" onclick="changeclassstyle()">
2
Hi
3
</a>