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.
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 .
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.
You can use "window. moveTo(X,Y)" to set the position of popup window.
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.
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>
You cannot customize or postion the default javascript prompt. Check this SO answer for workarounds.
How to customize the JavaScript prompt?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With