I've tried my best to solve my own problem but am having trouble getting started with a fairly simple JS script.
I'm trying to change the font colour of text which contains the "-" character for a specified element class.
This is what I've got:
<html>
<body>
<script>
<!--
function myFunction()
{
var elements = document.getElementsByClassName("example");
for(var i = 0, length = elements.length; i < length; i++)
{
if(elements[i].textContent.indexOf('-') != -1)
{
elements[i].style.color = red;
}
}
}
-->
</script>
<table>
<tr>
<td class="example">-100<td>
<td class="example">100<td>
</tr>
</table>
</body>
</html>
What have I done wrong?
Edit: this is the final code I went with, which works perfectly well.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
<!--
function myFunction()
{
var elements = document.getElementsByClassName("example");
for(var i = 0, length = elements.length; i < length; i++)
{
if(elements[i].textContent.indexOf('-') !== -1)
{
elements[i].style.color = "red";
} else
{
elements[i].style.color = "green";
}
}
}
-->
</script>
<table>
<tr>
<td class="example">-100<td>
<td class="example">100<td>
</tr>
</table>
<script type="text/javascript">
<!--
myFunction();
-->
</script>
</body>
</html>
.indexOf()is not being used properly It's a function call that takes a string and returns an integer.if(elements[i].textContent.indexOf('-') != -1)