Search code examples
cordovacordova-pluginsvisual-studio-cordova

cordova-plugin-dialogs:navigator.notification.prompt not working properly


 function onPrompt(results) {
   if (results.buttonIndex == 1) {
     alert('sucess');//working
   } else {
     alert('fail');//not working
   }
 }

navigator.notification.prompt(
  'Please enter your name',
  onPrompt,
  'Registration',
  ['Ok', 'Exit'],
  'Maruthi'
);

onprompt() else part is not working when user press exit but if is correctly working as its expected when user press on ok button.


Solution

  • You have to check the type of buttonIndex:

    function onPrompt(results) {
       if (results.buttonIndex === 1) {
         alert('sucess');
       } else {
         alert('fail');
       }
     }
    

    If you use only two equal signs, the 1 means TRUE, which is always TRUE in your function, because buttonIndex has a value.