jQuery function does not work inside if

Asked

Viewed 312 times

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();
});

Apparently correct but says the function cancelamento.css is not a function, someone knows what can be?

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

Thank you!

1 answer

3


When executing this line:

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

The cancel variable receives the #cancel Mes element, picks up its text and swaps all % for nothing, through the replace function. So cancellation is a string, you can be sure of it if you use a console.log(cancelamento);.

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

Your if would look like this:

if(cancelamento >= 4 && cancelamento <= 6){
    $("#cancelamentoMes").css({'background-color': 'yellow', 'color': 'black'});
}
  • thank you very much, it worked perfectly. I just didn’t understand how the function . css doesn’t handle string but it worked just by calling Elemental with id #cancel

  • The css function does not belong to the strings, you cannot change the css of a string, because a string does not have CSS, it is just text. You can change the CSS of an HTML element, such as a div. In this case, the #cancel Mes element. To catch this element you use $("#cancellations"). Understand?

  • Perfectly @André, grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.