miamiatz
JF-Expert Member
- Dec 4, 2017
- 505
- 941
Kiukweli kwa codes ndogo ndogo wala skuizi watu hawaumizi kichwa. We ieleze unachotaka inakupa code snippet unaenda ku paste mahala husika mambo yanaenda.
Niliwahi jaribu hii:
Ikanijibu hivi:
Niliwahi jaribu hii:
I have several html span elements with class tai-rates-perc in a page. I want to use pure JS to change color of the elements to green if value inside the element is equal to or greater than zero and change the color to red if the value inside the elements is less than zero. Kindly assist with complete code.
Ikanijibu hivi:
Here is the complete JavaScript code that you can use to change the color of the HTML span elements based on the value inside the elements:
Kiukweli hii kitu inatisha mana hilo jibu ni jibu zuri na moja ya njia za kufikia lengo la swali la hapo juu. Ni dhahiri writing codes inaenda kuathiriwa sana na ChatGPT.<script>
const elements = document.querySelectorAll('.tai-rates-perc');
for (const element of elements) {
const value = parseFloat(element.innerText);
if (value >= 0) {
element.style.color = 'green';
} else {
element.style.color = 'red';
}
}
</script>