Solved

JQuery suggestions for dynamic buttons in Modal Window

Posted on 2010-08-19
1
402 Views
Last Modified: 2013-11-11
Hi,
I have a standard for with standard HTML submit buttons etc.
When the page is loaded as a standalone page then showing these buttons is fine. I have made the script so only the form is loaded into a modal window, without the rest. In the modal  window the buttons are set using Jquery parameters.
What I would like to be able to do is load any form using the same script, but where it finds a html button in the form it replaces it with a modal window button and if its a submit button then it should submit when clicked. It should also hide the html buttons that its replacing.

The reason why I am trying to achieve this, is so I can re-use the same javascript to open any forms that are requested, rather than creating a function for each form and having to set up the buttons for each one.

Cheers
John
0
Comment
Question by:jdav357
1 Comment
 
LVL 15

Accepted Solution

by:
SRigney earned 500 total points
ID: 33476748
var formButtons = {};
$("#formtoload input[type=button]").each(function() {
   $currentButton = $(this);
   $currentButton.hide();
   formButtons = $.extend({
      $currentButton.value() : function(event, ui) {                   
                                      $currentButton.click();
                                            }
                                       }, formButtons ||{});
});

// add a default close button since the form did not have one previously.
formButtons = $.extend({
"Close": function(event, ui) {
                  $(this).dialog('destroy').remove();
            }
      }, formButtons ||{});

// now show the dialog with formButtons passed in to

you can then do something like
$('#formtoload').dialog({
            title: $('#formtoload').attr("title"),
            modal: true,
            position: 'top',
            close: function(event, ui) { $(this).dialog('destroy').remove(); },
            buttons: formButtons
      });
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question