JQuery: action as form is being submitted

jquery

Here's my problem:
I have a selectable grid of div's that are tied to unique category IDs

I could probably re-write the grid to use hidden inputs as they are being selected, but it's a little late for that.

As the form is submitted (standard form.. no ajax), I want to gather the selected IDs into a variable.. this code below works when I test with a standard button and alert box:

parentUID = "";
$('.cat_grid').each(function() {
  cat_parent = $(this).parent();
  if (cat_parent.attr('class')=="on") {
    parentUID += $(this).attr('id')+";";
  }
});

but how can I make this fire as the submit button is being clicked? I assume inserting parentUID into a hidden form input and using some sort of delay function??

Thanks for any answers!

Best Answer

How about:

 $(form).submit( function() {
   var parentUID = "";
   $('.cat_grid').each(function() {
    cat_parent = $(this).parent();
    if (cat_parent.attr('class')=="on") {
     parentUID += $(this).attr('id')+";";
    }
  });
  $(hiddenInput).val( parentUID );
  //don't return false because we wan't the form to be submitted
 });