d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Js Dialog
Add Reply New Topic New Poll
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jan 3 2013 06:51am
Is it posible to make JavaScript dialog with YES, NO and CANCEL buttons and assign custom tasks for each of selections? (Similar to confirm function, but with 3 options and possibly change to customize the names of buttons). I have a submit button that needs actions based on selections made.

Example code would be really nice if possible.

Thanks.

edit: MUST work with FF and IE, Chrome and Opera are plus.

This post was edited by vittujenkevat on Jan 3 2013 06:52am
Member
Posts: 14,235
Joined: Apr 20 2007
Gold: 15.00
Jan 3 2013 06:58am
javascript itself has no control like this, but there are several readytouse librarys you can find with google(one i would mention is jQuery dialog)
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jan 3 2013 07:15am
Quote (kuzdithom @ 3 Jan 2013 14:58)
javascript itself has no control like this, but there are several readytouse librarys you can find with google(one i would mention is jQuery dialog)


So basicly jQuery dialog is html page that is loaded as dialog if I got it right? How can I call it from JS, and I suppose it is possible to use return values with it? Or how should I use it in my purpose?
Member
Posts: 14,235
Joined: Apr 20 2007
Gold: 15.00
Jan 3 2013 07:27am
Quote (vittujenkevat @ Jan 3 2013 01:15pm)
So basicly jQuery dialog is html page that is loaded as dialog if I got it right? How can I call it from JS, and I suppose it is possible to use return values with it? Or how should I use it in my purpose?


http://jqueryui.com/dialog
there are many examples on the offical page

you need this one: http://jqueryui.com/dialog/#modal-confirmation
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jan 3 2013 07:40am
Quote (kuzdithom @ 3 Jan 2013 15:27)
http://jqueryui.com/dialog
there are many examples on the offical page

you need this one: http://jqueryui.com/dialog/#modal-confirmation


Found that page too, and ya the modal-confirmation one is what I need. But I dont know how to get it work with my page. Never done jQuery before, so little lost with it.

I have a form where I need the dialog when submit button is clicked. Is it possible to open the dialog from JS example?
Member
Posts: 14,235
Joined: Apr 20 2007
Gold: 15.00
Jan 3 2013 08:12am
Quote (vittujenkevat @ Jan 3 2013 01:40pm)
Found that page too, and ya the modal-confirmation one is what I need. But I dont know how to get it work with my page. Never done jQuery before, so little lost with it.

I have a form where I need the dialog when submit button is clicked. Is it possible to open the dialog from JS example?


post your code here
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 3 2013 08:27am
Do something like this

Code
<!doctype html>

<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>jQuery UI Dialog - Modal form</title>
   <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
   <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
   <script src="/resources/demos/external/jquery.bgiframe-2.1.2.js"></script>
   <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   <style>
       body { font-size: 62.5%; }
       label, input { display:block; }
       input.text { margin-bottom:12px; width:95%; padding: .4em; }
       fieldset { padding:0; border:0; margin-top:25px; }
       h1 { font-size: 1.2em; margin: .6em 0; }
       div#users-contain { width: 350px; margin: 20px 0; }
       div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
       div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
       .ui-dialog .ui-state-error { padding: .3em; }
       .validateTips { border: 1px solid transparent; padding: 0.3em; }
   </style>
   <script>
   $(function() {
       var name = $( "#name" ),
           email = $( "#email" ),
           password = $( "#password" ),
           allFields = $( [] ).add( name ).add( email ).add( password ),
           tips = $( ".validateTips" );

       function updateTips( t ) {
           tips
               .text( t )
               .addClass( "ui-state-highlight" );
           setTimeout(function() {
               tips.removeClass( "ui-state-highlight", 1500 );
           }, 500 );
       }

       $( "#dialog-form" ).dialog({
           autoOpen: false,
           height: 300,
           width: 350,
           modal: true,
           buttons: {
               "Create an account": function() {
                   var bValid = true;
                   allFields.removeClass( "ui-state-error" );

                   if ( bValid ) {
                       $( "#users tbody" ).append( "<tr>" +
                           "<td>" + name.val() + "</td>" +
                           "<td>" + email.val() + "</td>" +
                           "<td>" + password.val() + "</td>" +
                       "</tr>" );
                       $( this ).dialog( "close" );
                   }
               },
               Cancel: function() {
                   $( this ).dialog( "close" );
               }
           },
           close: function() {
               allFields.val( "" ).removeClass( "ui-state-error" );
           }
       });

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

<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 ui-widget-content ui-corner-all" />
       <label for="email">Email</label>
       <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
       <label for="password">Password</label>
       <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
   </fieldset>
   </form>
</div>


<div id="users-contain" class="ui-widget">
   <h1>Existing Users:</h1>
   <table id="users" class="ui-widget ui-widget-content">
       <thead>
           <tr class="ui-widget-header ">
               <th>Name</th>
               <th>Email</th>
               <th>Password</th>
           </tr>
       </thead>
       <tbody>
           <tr>
               <td>John Doe</td>
               <td>john.doe@example.com</td>
               <td>johndoe1</td>
           </tr>
       </tbody>
   </table>
</div>
<button id="create-user">Create new user</button>


</body>
</html>


This should open up a modal form on a button click and then add the details to the table.

Example pulled from here -> http://jqueryui.com/dialog/#modal-form
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Jan 4 2013 04:15am
Got it working now after few tries.
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
Jan 5 2013 02:22am
Yes, but you'll have to do a lot of it yourself, as previously stated js doesn't handle it natively. It is quite easy to accomplish with jQuery and modals.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll