0

I have read through loads of comments about how and why object expected can be reported, but I think my problem is that my javascript and calls are rubbish.

I want to build a simple function that, if I de-select the first check box, all other checkboxes are de=seleceted, or if I select one of the other checkboxes and not the forst check box, the first checkbox will automatically be selected.

I have this working fine when i use the checkbox input name in the javascript (all checkboxes except the first have the same name), but as I want to pass each input to a php function for processing, so I need different input names. So, in my wisdom, I thought why not add an id to each field and use the GetElementById function.

below is my attempt: ANY advice is more than welcome. Thank you.

<head>
<SCRIPT text/javascript>

function Check1(chk)
{
if(document.myform3.Check_ctr.checked==false){
	chk.checked = false ;
}
}

function Check2(chk)
{
chk.checked=true;
}

</script>
<body>
<form name="myform3" method="post" action="post_php">
<table align="center" width="370px" border="0">
<tr>
	<td width="250px">Item</td>
	<td  align="center" valign="middle">Tick to Display</td>
</tr>
<tr>    
        <td>Project Name: </td>
        <td align=center valign=middle><input type="checkbox" name="Check_ctr" value="yes" onclick="Check1(document.myform3.getElementById('check_list'))" /></td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="checkbox" value="1" id="check_list" onclick="check2(document.myform3.Check_ctr)"/></td>						
</tr>
<tr>
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="checkbox" value="1" id="check_list" onclick="check2(document.myform3.Check_ctr)"/></td>
</tr>
<tr>							
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="checkbox" value="1" id="check_list" onclick="check2(document.myform3.Check_ctr)"/></td>
</tr>
</table>
<input type="submit" name="submit" value="Save Changes" />

Edited by Thorby68: n/a

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Thorby68
0

First thing is you must write
Check2(document.myform3.Check_ctr) instead of
check2(document.myform3.Check_ctr) (Capital C in check2 because javascript is case sensitive.

I have changed whole code as following, keeping name and ID same, using checkbox array (easy to process in php after submission)

<head>
<SCRIPT text/javascript>

function Check1()
{
  var intCounter = 0;
   for(intCounter=0;intCounter<=document.myform3.elements.length-1;intCounter++)
    {
      if(document.myform3.elements[intCounter].type=="checkbox")
      {
          document.myform3.elements[intCounter].checked= document.myform3.Check_ctr.checked ;
       }
   }
}

function Check2(chk)
{
	
	if (chk.checked)
  	  document.myform3.Check_ctr.checked =true;
}

</script>
<body>
<form name="myform3" method="post" action="post_php">
<table align="center" width="370px" border="0">
<tr>
	<td width="250px">Item</td>
	<td  align="center" valign="middle">Tick to Display</td>
</tr>
<tr>    
        <td>Project Name: </td>
        <td align=center valign=middle><input type="checkbox" name="Check_ctr" id ="Check_ctr" value="yes" onclick="Check1()" /></td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="check_list[]" value="1" id="check_list[]" onclick="Check2(this)"/></td>						
</tr>
<tr>
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="check_list[]" value="1" id="check_list[]" onclick="Check2(this)"/></td>
</tr>
<tr>							
	<td>&nbsp;</td>
	<td valign=middle align=center><input type="checkbox" name="check_list[]" value="1" id="check_list[]" onclick="Check2(this)"/></td>
</tr>
</table>
<input type="submit" name="submit" value="Save Changes" />
0

Thank you urtrivedi,

This is quite amazing. I don't really understand everything that is going on in the line

for(intCounter=0;intCounter<=document.myform3.elements.length-1;intCounter++)

, however that will be my research project for today.

Many thanks for your invaluable help.

0

Many thanks urtrivedi,

I have found one small problem though, even though I have given each checkbox a unique name i.e. 1check_list[] and 2check_list[], the value for Check2 is returned as an array, instead of the value. Is there anything I am doing wrong here?

0

Ah,

Got it, if I remove the square brackets from the name field (of course ensuring the name is unique, and ensure the square brackets remain for the ID, I get a value and not an array.

Many thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.