Javascript - Prompt

About

Function Window.prompt will prompt you to enter a value with a question

Example

function setUserName(shouldPrompt) {
   
   var myName;
   if (shouldPrompt) {
       myName = prompt('Please enter your name.');
       localStorage.setItem('name', myName);  
   }
   
    if(!localStorage.getItem('name')) {
       myName = 'Anonymous';
    } else {
       myName  = localStorage.getItem('name');
   }
  
   var myHeading = document.querySelector('p');
   myHeading.textContent = 'Hello  ' + myName;
   
}

setUserName(false);

var myHTML = document.querySelector('button');
myHTML.onclick = function() {
    setUserName(true);
}
<p>Hello comes here</p>
<button>Change user</button>

Documentation / Reference





Discover More
Javascript - Input Stream

Javascript - Input Stream



Share this page:
Follow us:
Task Runner