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

 

 

 

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.