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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic initialisation of the counter

Status
Not open for further replies.

MikeyK

Programmer
Jul 29, 2001
7
AU
I am wondering if it is possible to have a dynamic counter
in a javascript function

I need to loop through that set of checkboxes, and check them or uncheck them. The checkboxes vary in amount and where on the page they start

I have the following code for this:

Code:
function WLToggle() {
	if (document.frmSearch.elements['chkWLToggle'].checked==false) {
		for(var i=0;i<10;i++)
			document.frmSearch.elements[i].checked=false;
	}
	else {
		for(var i=0;i<10;i++)
			document.frmSearch.elements[i].checked=true;
	}
}


The problem is that there isn't always ten check boxes on the page , and also the loop does not always start at the first check box on the page. So I need to find a way to dynamically set the initialisation of the counter, and also the i<n number to loop through

eg:

to start at the fifth of ten check boxes:
needs to be set to: for(var i=5;i<10;i++)

to start at the eighth of seventeen check box
needs to be set to: for(var i=8;i<17;i++)

I would like to achieve this with the one function if possible


I already have the following code
(from this forum) to build a dynamic array:

Code:
<HTML>
<HEAD>
<TITLE> Checkbox </TITLE>

<script>
function buildarray(frm) {
  //alert(frm.elements.length);
  //alert(frm.elements[0].type);
  arr = new Array;
  for (elm=0; elm<frm.elements.length; elm ++) {
    if (frm.elements[elm].type == &quot;checkbox&quot;) arr[elm] = frm.elements[elm].id;
    alert(arr[elm]);
  }
}
</script>
</HEAD>

<form name=&quot;myForm&quot;>

<input type=checkbox name='buildarray' id='buildarray' onclick=&quot;buildarray(document.myForm)&quot;></td>

 <input type=&quot;checkbox&quot; id=&quot;cbo3706&quot; name=&quot;cbo3706&quot;>cbo3706<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3707&quot; name=&quot;cbo3707&quot;>cbo3707<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3708&quot; name=&quot;cbo3708&quot;>cbo3708<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3709&quot; name=&quot;cbo3709&quot;>cbo3709<br>
<input type=&quot;checkbox&quot; id=&quot;cbo3710&quot; name=&quot;cbo3710&quot;>cbo3710<br>

<input type=checkbox name='buildarray' id='buildarray' onclick=&quot;buildarray(document.myForm)&quot;></td>

 <input type=&quot;checkbox&quot; id=&quot;cbo3711&quot; name=&quot;cbo3711&quot;>cbo3711<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3712&quot; name=&quot;cbo3712&quot;>cbo3712<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3713&quot; name=&quot;cbo3713&quot;>cbo3713<br>
 <input type=&quot;checkbox&quot; id=&quot;cbo3714&quot; name=&quot;cbo3714&quot;>cbo3714<br>
<input type=&quot;checkbox&quot; id=&quot;cbo3715&quot; name=&quot;cbo3715&quot;>cbo3715<br>
</form>
</BODY>
</HTML>

Any help would be appreciated
 
Have the initial value for the for loop variable and the constraining variable value be passed to the function as arguments. -gerrygerry
geraldschafer@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top