tags:

views:

118

answers:

6

If you look at the code inside the #top_cat and #high_hat sections below, it's obvious that it does the same thing.

$(document).ready(function ()
{

    $('#top_cat').submit(function ()
    {
        $.post($(this).attr('action'), $(this).serialize(), null, "script");
        return false;
    });

    $("#tip_click").click(function()
    {
        $("#high_hat").submit()(function ()
        {
            $.post($(this).attr('action'), $(this).serialize(), null, "script");
            return false;
        });
        return false;
    });

});

What's the correct syntax to separate the inner code (below) into its own function so that it's callable inside the #top_cat and #high_cat sections without the duplication that exists above?

   $.post($(this).attr('action'), $(this).serialize(), null, "script");
   return false;
+3  A: 
function submitHandler() {
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
}

$(document).ready(function () {

    $('#top_cat').submit(submitHandler);

    $("#tip_click").click(function() {
        $("#high_hat").submit(submitHandler);
    });

});
karim79
A: 

You might want to make a plugin that will do your submission:

;(function($){
    $.fn.extend({
        submitAction: function() {
            this.each(function() {
               $.SubmitAction(this);
            });
            return this;
        }
    });

    $.SubmitAction  = function(ctl) {
        $(ctl).filter('form').submit( function() {
            $.post( $(this).attr('action'), $(this).serialize(), null, 'script' );
            return false;
        });
    };     
})(jquery);

Called as:

$('#top_cat').submitAction();
tvanfosson
You came back. I was just trying out your original postAction() version which is a nice improvement over my current code. I have to read up on plugins - I don't know what to do with this - does it go inside the document ready section that I have above? The postAction function you wrote originally looked a lot simpler. Also, in your original answer, you made a point about returning false that I hadn't finished reading - if you still have the text of your original answer, would you mind posting it as a separate answer? Thanks.
It was essentially the same as Karim79's answer -- which is why I changed it. Obviously you wouldn't want to do the plugin route unless you needed to reuse this code often. The only reason I suggested the plugin route is that with jQuery you often find yourself rewriting basically the same thing. At some point it might make sense to refactor it to something you can call directly. Since you already had another answer showing you how to define the handler as a function, I offer this as an alternative when it makes sense to make it a full-fledged plugin.
tvanfosson
A: 

Your duplication is this part:

element.submit()(function ()
{
     $.post($(this).attr('action'), $(this).serialize(), null, "script");
     return false;
});
return false;

You can do: mysubmitfunc('#high_hat')

Where the function is the duplicated code.

James Black
A: 
$(document).ready(function(){
    $('#top_cat').submit(on_submit);

    $("#tip_click").click(function(){
        $("#high_hat").submit(on_submit);
        return false;
    });
});

function on_submit()
{
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
}
Michal M
+2  A: 

One cool part about jQuery is that it can select elements using any CSS selector and then attach functionality to the whole set. This includes multiple heterogeneous tags by id, so you could do this.

$("#top_cat,#high_hat").submit(function ()
{
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
});
joshperry
This is amazingly concise. I love it. I'm trying to think of drawbacks and I guess the only one might be if you have a hundred elements that all do the same thing, then you end up with a really long line of code :)
yes, that's true, but don't forget that you can also put multiple classes on an element, this is often used as a way to tag elements for future queries. `<li class="menu-item submits-form"/>` `<td class="data-item submits-form" />` both of these elements could be selected with `$(".submits-form").submit(...)`
joshperry
+1  A: 

I go completely the other way from the answers provided.....

I have this function in the js file:

$("form[rel=ajaxed]").submit(function(){
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;    
});

and then any form that needs to be submitted by ajax is defined like so:

<form action="someurl.com/file.php" method="post" rel="ajaxed">

notice the rel="ajaxed", and jQuery does the rest of the selector magic...

pǝlɐɥʞ