Get all Select Elements in a Form by referencing $(this) instead of $("form select")

Posted by DaveDev on Stack Overflow See other posts from Stack Overflow or by DaveDev
Published on 2010-03-05T11:03:49Z Indexed on 2010/03/23 6:03 UTC
Read the original article Hit count: 291

Filed under:
|

Hi Guys

I'm currently getting all the Select elements that exist in a form with the following:

$("form").submit(function(event)
{
    // gather data
    var data = GetSelectData($("form select"));

    // do submit
    $.post($(this).attr("action"), data, ..etc)
});

Instead of passing in $("form select"), is there a way I can say something like

$(this).children('select') // this doesn't work, btw

to get all the select elements that exist within the context of the form the submit event is executing for?

This will allow me to reduce my code to the following, moving all the functionality into a common function:

$("form").submit(function(event)
{
    GatherDataAndSubmit($(this));
});

function GatherDataAndSubmit(obj)
{
    var data = GetSelectData(obj.children('select'));

    $.post(obj.attr("action"), data, ..etc)
}

Thanks

Dave

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery