Help creating loop

Javascript coding ..

Moderators: macek, egami, gesf

lordNicholas
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Dec 19, 2011 11:25 pm

Help creating loop

Postby lordNicholas » Mon Dec 19, 2011 11:33 pm

Please, tell me how to make loop from these sript.
Here is the script, which only change on last numbers only, like :

document.formname.check11
document.formname.check21

document.formname.check12
document.formname.check22

document.formname.check13
document.formname.check23

document.formname.check14
document.formname.check24

document.formname.check15
document.formname.check25

the full script is :
<SCRIPT LANGUAGE="JavaScript"><!--

function codename() {
if(document.formname.check11.checked)
{
document.formname.check21.disabled=true;
}
else
{
document.formname.check21.disabled=false;
}
if(document.formname.check21.checked) {
document.formname.check11.disabled=true;
}
else
{
document.formname.check11.disabled=false;
}
if(document.formname.check12.checked)
{
document.formname.check22.disabled=true;
}
else
{
document.formname.check22.disabled=false;
}
if(document.formname.check22.checked) {
document.formname.check12.disabled=true;
}
else
{
document.formname.check12.disabled=false;
}
if(document.formname.check13.checked)
{
document.formname.check23.disabled=true;
}
else
{
document.formname.check23.disabled=false;
}
if(document.formname.check23.checked) {
document.formname.check13.disabled=true;
}
else
{
document.formname.check13.disabled=false;
}
if(document.formname.check14.checked)
{
document.formname.check24.disabled=true;
}
else
{
document.formname.check24.disabled=false;
}
if(document.formname.check24.checked) {
document.formname.check14.disabled=true;
}
else
{
document.formname.check14.disabled=false;
}
if(document.formname.check15.checked)
{
document.formname.check25.disabled=true;
}
else
{
document.formname.check25.disabled=false;
}
if(document.formname.check25.checked) {
document.formname.check15.disabled=true;
}
else
{
document.formname.check15.disabled=false;
}
}

//-->
</SCRIPT>
<form action="" method="" name="formname">
<p>
<input type="checkbox" onclick="codename()" name="check11" size="10">
<input type="checkbox" onclick="codename()" name="check21" value="ON">
</p>
<p>
<input type="checkbox" onclick="codename()" name="check12" size="10">
<input type="checkbox" onclick="codename()" name="check22" value="ON">
</p>
<p>
<input type="checkbox" onclick="codename()" name="check13" size="10">
<input type="checkbox" onclick="codename()" name="check23" value="ON">
</p>
<p>
<input type="checkbox" onclick="codename()" name="check14" size="10">
<input type="checkbox" onclick="codename()" name="check24" value="ON">
</p>
<p>
<input type="checkbox" onclick="codename()" name="check15" size="10">
<input type="checkbox" onclick="codename()" name="check25" value="ON">
</p>
</form>

mbrother
New php-forum User
New php-forum User
Posts: 45
Joined: Thu Sep 15, 2011 1:09 pm

Re: Help creating loop

Postby mbrother » Tue Dec 27, 2011 10:43 am

Change all <input> tags to include an ID attribute as follows:
<input type="checkbox" onclick="codename()" name="check11" id="check11" size="10">

Code: Select all

function codename()
{
   var anEl1, anEl2;
   var x;

   for (x=1; x < 6; ++x)
   {
      anEl1 = document.getElementById('check1' + x);
      anEl2 = document.getElementById('check2' + x);

      if (anEl1.checked)
        anEl2.disabled = true;
      else
        anEl2.disabled = false;

      if (anEl2.checked)
       anEl1.disabled = true;
      else
       anEl1.disabled = false;
   }
}


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 2 guests