Skip to content
Advertisement

change color when the number goes down

I am trying to change the class name to an element when its value goes down

My view in the blade is a foreach

JavaScript

I want to add a class to the td with id “changecolor”

My script is:

JavaScript

The color is applied only to the first element of the foreach and ignoring all the others.

I want to apply it to all foreach results that respect the if

Sorry for my bad English.

Advertisement

Answer

document.getElementById will always give you a single element. Most of the time the first element that it finds.

Instead of giving each element same id give them same name like

JavaScript

then use : document.getElementsByName("changecolor")

This will give all the elements with name ‘changecolor’.

You can loop through these elements and do the thing you want.

Your modified code will look something like this:

JavaScript
Advertisement