0

I wish I new how to even give a better title. But I have no idea even in which ballpark my problem is.

I'm trying to build something that allows me to ask "you really wanna?" after a user sends or saves a form. Here's my fiddle: http://jsfiddle.net/chrystalingus/zXUQ4/

$('.confirm_this').click(function(e){
    e.preventDefault();
    var modal_markup = '<div id="modal_confirm"></div>';
    $('body').append($(modal_markup));

            var send_button = $(this);
            var formular = send_button.parent();
            var formular_width = formular.outerWidth();
            var formular_height = formular.outerHeight();
            var reset_button_markup = '<input id="reset_button" type="reset" value="No">';
                $(reset_button_markup).insertAfter(send_button);
            var reset_button = $('#reset_button');
            var orig_send_value = send_button.attr('value');
            var form_marker_markup = '<div id="form_marker"></div>'; // placeholder for the form which temporarily goes into the modal overlay (and to find it's original location later)
                $(form_marker_markup).insertAfter(formular);
            var form_marker = $('#form_marker');
                form_marker.css({
                    'width': formular_width+'px',
                    'height': formular_height+'px'
                });
            var modal = $('#modal_confirm');

            send_button.unbind();
            formular.children().hide();
            send_button.attr('value', 'Yes').show();
            reset_button.show();
            formular.appendTo(modal);
            modal.show();

            reset_button.click(function(e){
                e.preventDefault();
                formular.insertBefore(form_marker);
                form_marker.remove();
                modal.remove();
                reset_button.unbind().remove();
                send_button.attr('value', orig_send_value).unbind();
                formular.children().show();
                confirm();
            });
});

This is supposed to work for any kind of form - no matter what the input types, the layout etc.

I'm fairly new to this and realize my jquery code is rather clumsy and crude - but should nonetheless work. So basically I'm trying to catch and prevent the click on the button class="confirm_this". The entire (hidden) form is being put up again in a modal overlay and the former "Send" button is now the "Yes" button.

It seems to work fine and if I had only one form on the page I guess the problem would never occur. But as soon as I have 2 forms on the page this happens: I click send on form A - modal - "no" - repeat - repeat then I click send on form B - modal - (all fine so far) - "no" ... Result: however often I repeated the process for form A - that's how many "No" buttons appear in the page next to form B. Also I have as many before body end. This happens only in browser, in jsfiddle I get an alert "undefined" but I have no clue what is not yet defined... Chrome console gives nothing at all.

Thanx for your help...

  • Do you need to make another modal? Can't you use a native "confirm" dialog? – Teh SoTo Jul 17 '14 at 07:24
  • That would be a JqueryUI component? I looked into that a while ago but never really got around to go into that further. So my plan was to build a custom solution consitent with the rest of the layout/design. I'd be happy to try out another solution. Any tips? – Aurel Engle Engelmann Jul 17 '14 at 07:32
  • No, just a native "confirm" dialog, check [this answer](http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box) – Teh SoTo Jul 17 '14 at 07:34
  • Alright, I found my solution: simplify ;) For some reason I couldn't get Teh SoTo's recommendation to work. It seems simple enough but none of the mentioned solutions worked at all. Maybe it is because I tried it with a local file?! I didn't try further and actually I don't think that could be the reason. Anyway, check out my fiddle for my working solution. So, much simpler... :) Thank you for your answer though. – Aurel Engle Engelmann Jul 17 '14 at 12:19

0 Answers0