Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

positioning the prompt popup in javascript

Tags:

javascript

i have a javascript prompt like the following and i would like to bring the prompt center of the screen. How to do this in using javascript?

function showUpdate() {     
    var x;    
    var name=prompt("Please enter your name","");

    if ( name != null ) {
      x="Hello " + name + "! How are you today?";
      alert("Input : " + name );          
    }

}

And this is how i call this :

<a onclick = "showUpdate() " style="vertical-align: middle;" class="parent" href=""><span>5. Missed P/U Comments</span></a>

it works find excepts the prompt goes to the left corner in IE and center in Firefox but i need to the same solution to work in both the browsers.

like image 591
Java Questions Avatar asked Dec 27 '12 10:12

Java Questions


People also ask

What is the significance of prompt dialog box explain?

Definition and Usage The prompt() method displays a dialog box that prompts the user for input. The prompt() method returns the input value if the user clicks "OK", otherwise it returns null .

How do you write a prompt in JavaScript?

The prompt() method in JavaScript is used to display a prompt box that prompts the user for the input. It is generally used to take the input from the user before entering the page. It can be written without using the window prefix. When the prompt box pops up, we have to click "OK" or "Cancel" to proceed.

How do I change the position of a pop up window?

You can use "window. moveTo(X,Y)" to set the position of popup window.

What does the prompt () in JavaScript do?

prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.


2 Answers

The prompt (and alert) popups are implemented differently depending on the browser you're using. This is because the popups are browser functionalities, they aren't JavaScript objects or anything like that. (Just like the console is different for each browser, it depends on the implementation.)

If you really want your prompts to be positioned / styled consistently, you're going to have to build your own prompt.

The easy way out would be to use a library like jQueryUI.

On the other hand, you can just build it yourself:

document.getElementById('showPromptButton').addEventListener('click', showSprompt);
document.getElementById('promptButton').addEventListener('click', submitPrompt);
var prompt = document.getElementById('myPrompt');
var promptAnswer = document.getElementById('promptAnswer');

function showSprompt() {
    promptAnswer.value = ''; // Reset the prompt's answer
    document.getElementById('promptQuestion').innerText = "What's your question?"; // set the prompt's question
    prompt.style.display = 'block'; // Show the prompt
}

function submitPrompt() {
    var answer = promptAnswer.value; // Get the answer
    prompt.style.display = 'none';   // Hide the prompt
    console.log(answer);
}
#myPrompt{
  display:none;
  /* Style your prompt here. */
}
<input id="showPromptButton" type="button" value="Show Prompt" />
<div id="myPrompt" class="proptDiv">
  <span id="promptQuestion"></span>
  <input id="promptAnswer" type="text" />
  <input id="promptButton" type="button" value="Submit" />
</div>
like image 70
Cerbrus Avatar answered Oct 28 '22 10:10

Cerbrus


You cannot customize or postion the default javascript prompt. Check this SO answer for workarounds.

How to customize the JavaScript prompt?

like image 27
Ravi Y Avatar answered Oct 28 '22 09:10

Ravi Y