Jump to content

[SOLVED] identify an input as part of a checkbox group dynamically


jug

Recommended Posts

Hi,

 

I need to be able to separate all form inputs into 3 groups, normal single inputs, radio buttons and checkbox groups. Obviously compiling the first 2 groups are easy but im struggling with checkbox groups.

 

The only information i wish to feed to the function is the form name and 3 arrays need to be returned with the forms inputs split into the groups i mentioned above.

 

var formName = "searchform";

var formElements = document.getElementById(formName).elements;

 

//from the form name compile 3 arrays of inputs name depending on type

 

var singleElements = new Array();  //single inputs (ie text, password, textarea, single checkboxes)

var radioElements = new Array();    //radio buttons

var checkElements = new Array();  //checkbox groups

 

I hope i have explained the issue well enough. Any other questions feel free to ask.

 

Thanks in advance.

 

jug

 

 

 

You should just name your checkbox "groups" with the same name that will be treated as an array by the processing page. You can then also reference these "groups" within the javascript.

 

Example:

<input type="checkbox" name-"somefield[]" value="1">
<input type="checkbox" name-"somefield[]" value="2">
<input type="checkbox" name-"somefield[]" value="3">

 

These can be referenced via javascript like this:

var formName = "searchform";
var formElements = document.getElementById(formName).elements;
var checkBoxGroup = formElements['somefield'];
var firstCheckbox = checkBoxGroup[0];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.