Search code examples
javascripthtmlpromptconfirm

prompt (true and false, not work)!


I have two option in prompt, acept and cancel, here is ok, but when I do cancel in prompt, this generate a list of anyway, why not function?

  add.onclick = function(){
   if(true) {
    var itemText = prompt('Create a Memo');
   }else {
    return false;
  }

Solution

  • var question = prompt('Create a memo');
    if(question) {
        alert('true - ' + question);
    } else {
        alert('false - ' + question);
    }
    

    DEMO

    Another example:

    var question = prompt('Create a memo');
    if(question) {
        document.getElementById('text').innerHTML = 'I answered ' + question;
    } else {
        document.getElementById('text').innerHTML = 'I canceled';
    }
    

    DEMO 2