PDA

View Full Version : checkbox array



hemi519
06-08-2011, 05:56 AM
Hi All,

I am struck with javascript functionality. Can any one help me out from the following scenario.

I am having a td where iam getting some names using foreach.


<td align='center'> <input type="checkbox" name="variant[]" id="<? echo $Variant[Name]; ?>" value="<? echo $Variant[Name]; ?>"></td>

iam having three different types of names and iam adding a new row when ever any row is inserted in database. iam checkaa functionality for every column.

column A columnB columnC
checkbox checkbox

checkbox checkbox

checkbox

button1 button 2 button3

the above is my scenario.

all the checkbox i am getting is from the above td i wrote. if i select button1 i should be able to select all the checkboxes under column A. and the same for button2 and button 3. How can i do this.

jscheuer1
06-08-2011, 02:43 PM
Is there something about the buttons that ties or relates them to the checkboxes, like a shared class that no other input elements share, or are thay all the children of a parent element that contains no other checkboxes?

If you don't know for sure, we need to see the actual generated HTML markup.

If you do know, what is it?

Alternatively we could create something like that as they are being retrieved/created by the server side code.

We may still need to see the HTML code, better yet a link to the page.

And as a side note, would jQuery be OK? In most cases it would be easier to write something like this in jQuery.

vwphillips
06-08-2011, 03:40 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function Check(cb){
var td=cb,table=cb,checked=cb.checked,index,rows,cells;
while (td.parentNode&&td.nodeName.toUpperCase()!='TD'){
td=td.parentNode;
}
index=td.cellIndex;
while (table.parentNode&&table.nodeName.toUpperCase()!='TABLE'){
table=table.parentNode;
}
rows=table.rows;
for (var z0=0;z0<rows.length;z0++){
cb=rows[z0].cells[index].getElementsByTagName('INPUT')[0];
if (cb){
cb.checked=checked;
}
}
}

/*]]>*/
</script></head>

<body>
<table border="1">
<tr>
<td><input type="checkbox" name="" onclick="Check(this);" /></td>
<td><input type="checkbox" name="" onclick="Check(this);" /></td>
</tr>
<tr>
<td><input type="checkbox" name="" onclick="Check(this);" /></td>
<td>''<td>
</tr>
<tr>
<td><input type="checkbox" name="" onclick="Check(this);" /></td>
<td><input type="checkbox" name="" onclick="Check(this);" /></td>
</tr>
</table>
</form>
</body>

</html>