samedi 18 juillet 2020

Change text color using if/else in javascript

I hope someone can help me with this.I am new in javascript. I am trying to change the color of a text depending on the status in

<td id="payment_status_cl" style="text-align: center; height:25px"> <%=resultSet.getString("payment_status")%></td>

It doesn't work if I insert if/else statement in the javascript. But it will work if there are no if/else statement. For example:

This one only change the color of one status:

function f_color(){
if (document.getElementById('payment_status_cl').value = 'Submitted') {
document.getElementById('payment_status_cl').style.color = "Green";}
}
f_color();

This didn't work. javascript:

<script>
function f_color(){
if (document.getElementById('payment_status_cl').value = 'Submitted') {
document.getElementById('payment_status_cl').style.color = "Green";
}
else if (document.getElementById('payment_status_cl').value = 'Pending') {
document.getElementById('payment_status_cl').style.color = "Orange";
}
else if (document.getElementById('payment_status_cl').value = 'Warning'{
document.getElementById('payment_status_cl').style.color = "Yellow";
}
else if (document.getElementById('payment_status_cl').value = 'Overdue') {
document.getElementById('payment_status_cl').style.color = "Red";
}
}
f_color();
</script>

I already tried by adding div.

var status = document.getElementById("payment_status_cl");
function fcolor() { 
    if (status === 'Submitted') {
      document.getElementById("centerbox1").style.backgroundColor = '#99C262';
    }
    else if (status === 'Pending')
    {
      document.getElementById("centerbox1").style.backgroundColor = '#F8D347';
    }  
    else if (status === 'Warning')  
    {
      document.getElementById("centerbox1").style.backgroundColor = '#FF6C60';
    }
  }();

but it doesn't work. I appreciate all the help given.

Aucun commentaire:

Enregistrer un commentaire