Skip to content

Instantly share code, notes, and snippets.

@varnie
Created September 8, 2011 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varnie/1203159 to your computer and use it in GitHub Desktop.
Save varnie/1203159 to your computer and use it in GitHub Desktop.
an example of changing a style of td using javascript and DOM
<html>
<head>
<title>getElementById example</title>
<style>
table .c { color: red }
.a.c {color: green}
</style>
<script type="text/javascript">
function changeStyle(theId){
var table = document.getElementById(theId);
var cell = table.getElementsByTagName("td")[0];
if (cell.className=="a c"){
cell.className = "c";
} else{
cell.className="a c";
}
}
</script>
</head>
<body>
<table id="t">
<tr>
<td class="c">Текст</td>
</tr>
</table>
<button onclick="changeStyle('t');">change color</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment