javascript - How to change text colour based on conditions?


Changing the colour of the marks obtained by a student - if < 40 then red else black.

I have a table in a PHP file displaying marks obtained in various subjects. Below 40 marks should be shown in red.

echo "<td>".$res['marks1i']."</td>";
echo "<td>".$res['marks1e']."</td>";
echo "<td>".$res['marks2i']."</td>";
echo "<td>".$res['marks2e']."</td>";
echo "<td>".$res['marks3i']."</td>";    
echo "<td>".$res['marks3e']."</td>";



Using Javascript:

You can add a class name to each of thetd's that have marks (say 'marks'), then:

var marks = document.getElementsByClassName('marks');
for (const element in marks) {
  if(element.innerHTML < 40) { = 'red';



Would that solve your problem?:

foreach ($res as $mark)  echo '<td class="page_speed_380721913">'.$mark.'</td>';

People are also looking for solutions to the problem: php - PDO with MSSQL returns Invalid Cursor


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.