42

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);
TN888
  • 7,659
  • 9
  • 48
  • 84

4 Answers4

31

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

NDM
  • 6,731
  • 3
  • 39
  • 52
6

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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>
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>
phuzi
  • 12,078
  • 3
  • 26
  • 50
  • 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](https://stackoverflow.com/review/low-quality-posts/22170327) – Nick Feb 11 '19 at 01:54