43

Is there any way to create prompt in JavaScript with two input fields ?

I tried that code, but it didn't help me :

var a = prompt("A : ", "");
var b = prompt("B : ", "");
alert(a + "\n" + b);

4 Answers 4

32

This is not possible with an OS or native browser window popping up. You will have to create a custom overlay dialog.

I would advise using a library like jQuery UI to do this. You can then customize whatever is in the popup.

You can view a demo of the dialog here

6

Short of constructing your own using DOM methods and Input elements: No.

2

JavaScript Code

           <script>
             $( "#create-user" )
             .button()
             .click(function() {
             $( "#dialog-form" ).dialog( "open" );
               });
            });
           </script>

Html Code:

          <div id="dialog-form" title="Create new user">
          <p class="validateTips">All form fields are required.</p>
          <form>
         <fieldset>
         <label for="name">Name</label>
          <input type="text" name="name" id="name" class="text">
          <label for="email">Email</label>
         <input type="text" name="email" id="email" value="" class="text">
         <label for="password">Password</label>
          <input type="password" name="password" id="password" value="" class="text">
        </fieldset>
        </form>
       </div>  

      <button id="create-user">Create new user</button>
1
  • 13
    this uses the jQuery library.
    – zoranc
    Mar 9, 2014 at 11:55
2

function abrete() {
  $("#dialog").dialog();
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="dialog" title="Create new user" style="display:none;">
  <p class="validateTips">All form fields are required.</p>
  <form action='tuscript.php'>
    <fieldset>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" class="text">
      <label for="email">Email</label>
      <input type="text" name="email" id="email" value="" class="text">
      <label for="password">Password</label>
      <input type="password" name="password" id="password" value="" class="text">
    </fieldset>
  </form>
</div>
<button onclick='abrete()'>Create new user</button>

1
  • 11
    Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. From Review
    – Nick
    Feb 11, 2019 at 1:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.