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

Checking checkbox value against array, show element 2

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
It seems to me that this should work. I'm not getting an error, and values seem to be passing through, but still nothing happens.

Can someone see something obviously wrong?

When a checkbox is clicked, the function checks the value against an array. If true, it should display a table row.

Code:
<input type="checkbox" name="arr_groups_id" value="31" onclick="checkGroupForEList(this.value);" />

        <tr id="row_elist" style="display:none;">
          <td>...</td>
        <td>...</td>
        </tr>

function checkGroupForEList(group_id) {
			if ( checkArray(group_id,'30,31,38') ) 
			document.getElementById('row_elist').style.display = '';
		}

function checkArray(matchVal,theArray)
{
   var match=false;
   var myArray= new Array (theArray)
   for (i=0;i<myArray.length;i++)
   {
      if (myArray[i]==matchVal)
      {
         match=true;
      }
   }
   return match;
}
 
Also your array creating is flawed. You have only one element with a string of "30,31,38". Change this line:

Code:
var myArray= new Array (theArray)

to this:

Code:
var myArray = theArray.split(',');

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You can also optimise it further by changing this:

Code:
match=true;

to this:

Code:
match = true;
break;

as there is no point in continuing the iteration once you've found a positive match.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top