tags:

views:

332

answers:

2

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!

+2  A: 

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
 });
Pim Jager
thanks!! I wasn't even aware of the submit Event.
stunnaman
No problem. Glad I could help!
Pim Jager
A: 

You can use onSubmit on the form and call function GetParentUIDs() with the code you have, plus storing IDs in the hidden field. Your GetParentUIDs must return true for the form to submit.

Example

<form name="yourform" action="youraction" method="post" onsubmit="return GetParentUIDs();">
Dinci Garrone