0

in simple javascript code the input of the prompt box work properly

f = prompt ("write a number here") 
   r = 1;
   x = f-r;
   alert(x);

when style the prompt box with sweetalert the result will be NaN before you write the input value

  f = swal({
  title: "An input!",
  text: "Write something interesting:",
  type: "input",
  showCancelButton: true,
  closeOnConfirm: false,
  animation: "slide-from-top",
  inputPlaceholder: "Write something"
},
function(inputValue){
  if (inputValue === false) return false;

  if (inputValue === "") {
    swal.showInputError("You need to write something!");
    return false
  }

  swal("Nice!", "You wrote: " + inputValue, "success");
});

   r = 1;
   x = f-r;
   alert(x);
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • 1
    Of course it is, thanks - didn't read properly... @AlonEitan – baao May 03 '17 at 19:11
  • OP: Please note that although that duplicate is about AJAX async request, it's still very much related - You need to put that code in the second example **inside** the callback function (Where you validate the `inputValue` parameter), because it's available only after the user click on the "OK" button. Unlike `prompt` that stops the code from execute until the the user enter a value, swatalert will allow the code to run in the background – Alon Eitan May 03 '17 at 19:16
  • @AlonEitan when i did that it it be available only after the user click on the "OK" button , but the return still allways NaN – fivefour11fgf May 03 '17 at 19:27
  • @SlimSoussia Put that code right after `swal("Nice!", "You wrote: " + inputValue, "success");` and before (`});`), something like `r = 1;f=parseInt(inputValue); x = f-r;alert(x);` – Alon Eitan May 03 '17 at 19:29
  • @AlonEitan thank you this solved the probleme , inputvalue wasn't int its value was an object ? – fivefour11fgf May 03 '17 at 19:38
  • @SlimSoussia No, it's `` the input you see on sweetalert, so it return a **string** - `"8"` instead of `8` (for example) – Alon Eitan May 03 '17 at 19:41

0 Answers0