I am using prompt box and if I press cancel without entering data in textbox of promptbox then the my further logic after prompt box do not execute ;if I press ok the that code is executed. how to make my code execute even if I press cancel in promtbox as it happen after I enter data and press ok ?
var textentered=prompt("Enter text/int:","");
if(textentered.length > 0){
alert(textentered);
}
// this is just example code below the promptbox which is not executed after i press //cancel in prompt box
text=[];
array=[];
document.getElementById('test').innerHTML='';
The problem if when prompt
is canceled, textentered
would be null
.
Thus, the if(textentered.length > 0)
will fail.
You'll have to change the code to act differently if textentered
is null
.
Like below example.
var textentered = prompt("Enter text/int:", "");
if (textentered) { //pressed ok OR if text is not empty
alert(textentered);
} else { //pressed cancel
textentered = ""; //is null. change to empty string
}
//at this point, non-empty text means that user pressed the ok button