views:

48

answers:

4

Hi.

I'm new in jquery/javascript and couldn't google for any solution.

I have number of divs generated automatically from cycle. Each of them contains form as a child element. So on form submit I'd like the corresponding parent div to fadeout.

I am using Ajax for form submission. Basically, now I have an event handler 'submit' binded to all child forms within divs. Here what it looks like

        $('#divCreatedActivities').children('div').children('form').submit(function() {
            var options = { 
                    target:        $('#divCreatedActivities').children('div'), 
                    dataType:      'xml',
                    success:       submittedActivity
            };
            $(this).ajaxSubmit(options); 
            return false; 
        });

        function submittedActivity() {
            alert('ahoy!');     
        }

How can I do that with JQuery?

Thank you.

+1  A: 
$('form').submit(function(){
    $(this).parent().fadeOut();
});

That's the way to do it. But I have a question, how can you see it fading when it's submitting?? Isn't it reloading or something? Are you using ajax?


based from your edited post,

$('#divCreatedActivities form').submit(function() {
    var $this = $(this);
    var options = { 
         target:        $('#divCreatedActivities').children('div'), 
         dataType:      'xml',
         success:       function() {
                     $this.parent().fadeOut();
                     alert('ahoy!');    
         }
    };
    $this.ajaxSubmit(options); 
    return false; 
});

/* remove this
function submittedActivity() {
    alert('ahoy!');     
} */

Welcome to stackoverflow.com
Don't forget to accept an answer

Reigel
yes, I am using Ajax. I updated a question with piece of code
Paulus
see my edits...
Reigel
Thank you! $this.parent().fadeOut(); throws an error on $this. I made it $(this), alert is firing, but still no fadeOut. Maybe it has something to do with live() function, since divs are generated on the fly? edit: missed the line var $this = $(this). Works as a charm with this! Thank you very much!!!
Paulus
A: 
$('#my-form-id').submit(function(){ $(this).parent().fadeOut(600); });
Thomas Clayson
there's no specific #my-form-id, since I don't know in advance how many forms will be generated. Number of forms depends on previous selection, that user has made.
Paulus
+1  A: 
T.J. Crowder
I think you can put the submitting part in callback of fadeOut.
livibetter
`return false;` on the `form` was what I had in mind. But it did not work for me. [fiddle](http://jsfiddle.net/dqxEw/)
Reigel
thank you, but I think sticking to '#id' is not an option in this case, since I don't know in advance on which div event will fire. Or maybe I just dont get it :)
Paulus
@Paulus: I've updated the answer to explain a bit more and to demonstrate/discuss using `parents` or `parent` to find the relevant div.
T.J. Crowder
@Reigel seems this code has a slight issue, I unbind `submit` event to get it work, see http://jsfiddle.net/dqxEw/1/ If we don't do so, I think this event handler will be invoked every 0.5 seconds. Another way to work around is to check if parent has been faded or not, if it has, then `return true;`
livibetter
@Paulus: Added a live example
T.J. Crowder
@T.J. I totally forgot that, stuck in jQuery world. :)
livibetter
@livibetter: *"I think you can put the submitting part in callback of fadeOut."* Good idea, done. And you're right about the submit handler getting triggered again when using the jQuery `submit` (I ran into it as well); that's a jQuery thing. Your way of solving it works, as does mine above (using the DOM element's `submit` rather than jQuery's; the DOM element's `submit` does not call submit handlers, by design). See the working live example.
T.J. Crowder
@livibetter: Just different ways of solving the same problem. :-)
T.J. Crowder
A: 

You can use jQuery.parent() or jQuery.parents() to find out the div that encapsulates the form(s).

<div class="form-wrap">
    <!-- you can use .parent() here -->
    <form>
    </form>
</div>

<div class="form-wrap">
    <!-- you should use .parents() here -->
    <div class="some-class">
        <div class="some-other-class">
            <form>
            </form>
        </div>
    </div>
</div>
Salman A