Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incrementing all VALUE attributes 1

Status
Not open for further replies.

Arzo2000

Programmer
Jul 7, 2005
12
CA
Hi all,
I have a lot of checkboxes on a form and I would like to set a unique value to their VALUE attribute. So basically what I am after is a script that parses through all <INPUT> tags of TYPE "checkbox" and sets the VALUE attribute to a unique number (I suggested that it would be incremented).

Here is an example of what I am after:
Before:
Code:
<INPUT ID="chkOption1" TYPE="checkbox" VALUE="checkbox" />
<INPUT ID="chkOption2" TYPE="checkbox" VALUE="checkbox" />
<INPUT ID="chkOption3" TYPE="checkbox" VALUE="checkbox" />

After:
Code:
<INPUT ID="chkOption1" TYPE="checkbox" VALUE="1" />
<INPUT ID="chkOption2" TYPE="checkbox" VALUE="2" />
<INPUT ID="chkOption3" TYPE="checkbox" VALUE="3" />

Thanks in advance guys.

 
1) get a collection of form elements:

Code:
var myElems = document.forms['myformname'].elements;

2) set an initial index:

Code:
var intIndex = 1;

3) loop through all elements. if it's a checkbox, set the value then increment the counter:

Code:
for ( var i = 0; i < myElems.length; i++ ) {
    if ( myElems[i].type == 'checkbox' ) {
        myElems[i].value = intIndex;
        intIndex++;
    }
}



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top