Why Doesn't My Dialog Close On The Button Click?

Why Doesn't My Dialog Close On The Button Click?

Hi guys,

I have jQuery code that's executed on a button click which brings up modals based on the validity of the user input. I have buttons on the modals that when clicked should close the modals, I have 4 different if statements and the modals close when the button is clicked on all the modals except the one in the last if block and I have no idea as the code is the same, what is wrong here? Here's my code below

  1. $(function(){

  2. $("input[name=addAnotherExpense]").on("click", function(e){
  3. e.preventDefault();
  4. var expenseDetail = $("input[name=expenseDetail]").val();
  5. var expenseAmountVal = $("input[name=expenseAmount]").val();
  6. if (expenseAmountVal.length > 10) 
  7. {
  8. console.log("expenseAmountVal > 3");
  9. $('#myDialog1').dialog({
  10. title : "Error",
  11. hide: "fadeOut",
  12. show: "fadeIn",
  13. buttons: {
  14. "ok" : function(){ $(this).dialog('close'); },
  15. "member" : function() { console.log("member button clicked"); }
  16. }
  17. });
  18. }
  19. else if (expenseDetail.length < 1)
  20. {
  21. $('#myDialog2').dialog({
  22. modal: "true",
  23. title: "Error",
  24. show: "fadeIn",
  25. hide: "fadeOut",
  26. buttons: {
  27. "ok" : function(){ $(this).dialog('close'); }
  28. }
  29. });
  30. }
  31. else if (expenseAmountVal == 0) 
  32. {
  33. $('#myDialog3').dialog({
  34. title: "Error",
  35. modal: "true",
  36. show: "fadeIn",
  37. hide: "fadeOut",
  38. buttons: {
  39. "ok" : function(){ $(this).dialog('close'); }
  40. }
  41. });
  42. }
  43. else if (expenseDetail.length > 0 && expenseAmountVal.length <= 10 && expenseAmountVal > 0)
  44. {
  45. (expenseAmountVal > 0) ? expenseAmount = expenseAmountVal : expenseAmount = 0.00;
  46. AddAnotherExpense(expenseDetail, expenseAmount);
  47. for(i=0, total=0; i<newArray.length; i++)
  48. {
  49. amount = 0.00;
  50. amount = newArray[i].amount;
  51. total = parseFloat(total) + parseFloat(amount);
  52. console.log("total in for loop = " + total.toFixed(2));
  53. details = newArray[i].detail;
  54. console.log(newArray[i]);
  55. $("#transactions").append(" " + details);
  56. }
  57. $('#myDialog4').dialog({
  58. title: "Item added",
  59. modal: "true",
  60. show: "fadeIn",
  61. hide: "fadeIn",
  62. buttons: {
  63. "ok" : function(){ $(this).dialog('close'); console.log(this); }
  64. }
  65. });
  66. $("#totalExpensesParagraph").append().text("Total expenses for this month R " + parseFloat(total).toFixed(2));
  67. //$("input[name=expenseAmount]").val("placeholder='0.00'");
  68. //$("input[name=expenseDetail]").val("");

  69. }

  70. });

  71. });