Fred Dompierre Fred Dompierre - 20 days ago 5
Javascript Question

Integer not being interpreted

This code doesn't seem to understand the integer in the var.

var btndelivery = $j('.delivery').parent("li");
var grandtotaltxt = $j(".gtman").text().replace("$", "");
var grandtotal = parseInt(grandtotaltxt);
alert(grandtotal);
$j(document).ready(function() {
if ($j(grandtotal) > 60) {
alert("plus grand");
$j(btndelivery).show();
} else {
alert("plus petit");
$j(btndelivery).hide();
}
})


It always alerts
plus petit
.

Answer

The variable grandtotal was already parsed into an integer. It looks like you need to remove the jQuery wrapping to compare it to another integer. Also, if $j(".gtman").text() comes back undefined then the replace function will throw an error. It's probably good to wrap the code in an if statement.

if ($j(".gtman").length) {
  var btndelivery = $j('.delivery').parent("li");
  var grandtotaltxt = $j(".gtman").text().replace("$", "");
  var grandtotal = parseInt(grandtotaltxt);
  alert(grandtotal);

  $j(document).ready(function() {
    if (grandtotal > 60) {
      alert("plus grand");
      $j(btndelivery).show();
    } else {
      alert("plus petit");
      $j(btndelivery).hide();
    }
  });
}