from Javascript to jQuery

Asked

Viewed 43 times

1

Well I have a question about jQuery, what the following code would look like Javascript Puro using the jQuery?

function writeTotal(value){
    var total = document.getElementById('total');
    total.innerHTML = floatToMoneyText(value);
}

2 answers

2


A reduced form:

function writeTotal(value) {
    $('#total').html(floatToMoneyText(value));
}

That one floatToMoneyText probably could also be converted to jQuery, but would have to know what you do exactly in it

1

How about testing the parameter value to see if it is null/empty:

function writeTotal(value) {

var innerHtml = "";

if (value === window.undefined || value === null || value === "") {
    innerHtml = "R$ 0,00";
}
else {
    innerHtml = floatToMoneyText(value);
}

$('#total').html(innerHtml);
};
  • 1

    The body of the function could be replaced by $('#total').html(value ? floatToMoneyText(value) : "R$ 0,00");, but I think that would be the responsibility of the function floatToMoneyText

  • Clear-cut! use a ternary condition It’s a good one, particularly I like to check whether it’s null or empty, makes it easier for human reading. I also agree, create a validation within the function floatToMoneyText.

Browser other questions tagged

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