jQuery function does not work inside if

0

I have the following code:

function formataPorcentuaisMensais(){
    var cancelamento = $("#cancelamentoMes").text().replace('%','');
    var aproveitamento = $("#aproveitamentoMes").text().replace('%','');
    var primCompras = $("#primComprasMes").text().replace('%','');
    var juros = $("#jurosMes").text().replace('%','');
    var seguros = $("#segurosMes").text().replace('%','');

    if(cancelamento >= 4 && cancelamento <= 6){
        cancelamento.css({'background-color': 'yellow', 'color': 'black'});
    }
}

$(function(){
formataPorcentuaisMensais();
});

It seems to be correct but says that the function cancelamento.css is not a function, does anyone know what it can be?

* I have loaded jquery; ** All variables are working.

Thank you!

    
asked by anonymous 10.01.2017 / 22:54

1 answer

3

When you run this line:

var cancelamento = $("#cancelamentoMes").text().replace('%','');

The cancellation variable receives the #cancelamentoMes element, takes its text and changes all% for nothing, through the replace function. So, cancellation is a string, you can be sure of that if you use console.log(cancelamento); .

When using cancelamento.css() you are calling the css function, but this function is not a string function. The correct would be: $("#cancelamentoMes").css();

Your if would look like this:

if(cancelamento >= 4 && cancelamento <= 6){
    $("#cancelamentoMes").css({'background-color': 'yellow', 'color': 'black'});
}
    
10.01.2017 / 23:15