Fred Dompierre Fred Dompierre - 22 days ago 6
Javascript Question

JQuery - Integer doesnt seem to be interpreted

In this code, the JQuery doesnt 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 return "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();
    }
  });
}