Question

So I am generating a form that is different depending on what the user has chosen. Because of this, I have no idea what is on the page (i.e. select, checkboxes, radio, textarea, text input).

I need a way to just see if a group of radios has been checked or not, essentially to return the value of those radios that are all in the same group. Currently I'm using this jquery each loop:

  $("#formdiv input:radio").each(function(){
  //random validation code here
  }

The problem is that makes me go through every single radio button, when I just want to go through them by how they are grouped.

Additionally, I also need to get checkboxes by their group to get all of the checked values, but I need to know which ones are in the same checkbox group. My current each loop just returns all the checkboxes as individual things:

  $("#formdiv input:checkbox").each(function(){
  //random validation code here
  }

Can anyone help me out?

Sample HTML:

<div class='formatdiv'>
    year: <select name='year' class='required' id='validation3:select'>
        <option value=''></option>
        <option value='1992'>1992</option>
        <option value='1993'>1993</option>
        <option value='1994'>1994</option>
        <option value='1995'>1995</option>
        <option value='1996'>1996</option>
        <option value='1997'>1997</option>
        <option value='1999'>1999</option>
    </select>
</div>
<div class='formatdiv'>
    color: <input type='text' name='color' class='required' id='validation1:text' />
</div>
<div class='formatdiv'>
    used: no <input type='radio' class='optional' id='novalidation4:radio' name='used' value='no' />
    yes <input type='radio' class='optional' id='yesvalidation4:radio' name='used' value='yes' />
</div>
<div class='formatdiv'>
    Checks: Check 1 <input type='checkbox' class='required' name='Checks' value='Check 1' />
        check 2 <input type='checkbox' class='required' name='Checks' value='check 2' />
       check 3 <input type='checkbox' class='required' name='Checks' value='check 3' />
</div>
Was it helpful?

Solution

As Matt pointed out, the answer can be found here:

jQuery and radio button groups

With some simple modification this should also work for checkboxes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top