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

Enable checkboxes

Status
Not open for further replies.

macluv22

Programmer
Joined
Feb 28, 2002
Messages
7
Location
NL
Hi,

I hope someone can solve my problem:

I tried the following code to enable some checkboxes after I check one checkbox...

Unfortunately it doesn't work, and I can't figure out what the problem exactly is.

I hope someone can help me. Thanks anyways...

-----------------
Code:
<html>
<head>

	<script language='javascript'>
	function markall(var)
	{ 	
	  f=document.forms[0];
	 	
	  for(i=0;i<f.elements.length;i++)
	  {
	    if(f.elements[i].id ==var) 
	       f.elements[i].disabled = false;
	  }
	}
	</script>

</head>
<body>

  <form name=&quot;myform&quot; action=&quot;test4_x.php&quot; method=&quot;post&quot;>
    <b>menu</b><br>
    <input type=&quot;checkbox&quot; onclick=&quot;markall(abc)&quot; name=&quot;def&quot; value=&quot;1&quot;>main menu<br>
    <input type=&quot;checkbox&quot; id=&quot;abc&quot; value=&quot;2&quot; disabled>submenu 1<br>
    <input type=&quot;checkbox&quot; id=&quot;abc&quot; value=&quot;3&quot; disabled>submenu 2<br>
    <input type=&quot;checkbox&quot; id=&quot;abc&quot; value=&quot;4&quot; disabled>submenu 3<br>
    <input type='submit' value='go'>
  </form>

</body>
</html>
 
it seems you have many items with the same id. Ids are a UNIQUE identifier so all items should be have a different one.

Another thing I would point out is that var is a keyword in Javascript. It is used to create a variable so it cannot be a variable name.

Here is a fixed up version of your code as best I see it :

<html>
<head>

<script language='javascript'>
function markall(name, flag)
{
var f = document.forms[0];

for(i = 0; i < f.elements.length; i++)
{
if(f.elements.id.indexOf(name) != -1)
{
f.elements.disabled = !flag;
}
}
}
</script>

</head>
<body>

<form name=&quot;myform&quot; action=&quot;test4_x.php&quot; method=&quot;post&quot;>
<b>menu</b><br>
<input type=&quot;checkbox&quot; onclick=&quot;markall('abc', this.checked)&quot; name=&quot;def&quot; value=&quot;1&quot;>main menu<br>
<input type=&quot;checkbox&quot; id=&quot;abc1&quot; value=&quot;2&quot; disabled>submenu 1<br>
<input type=&quot;checkbox&quot; id=&quot;abc2&quot; value=&quot;3&quot; disabled>submenu 2<br>
<input type=&quot;checkbox&quot; id=&quot;abc3&quot; value=&quot;4&quot; disabled>submenu 3<br>
<input type='submit' value='go'>
</form>

</body>
</html> Gary Haran
==========================
 
the first problem is that you're using the same [tt]id[/tt] more than once. an ID should NEVER be used more than once - you can use the same NAME more than once.

second problem is this line:[tt]
function markall(var)[/tt]

'var' is a reserved word in javascript - you cannot use it as a variable or argument name.

third problem is this line:[tt]
<input type=&quot;checkbox&quot; onclick=&quot;markall(abc)&quot;[/tt]

you need to pass the argument as a string, unless &quot;abc&quot; is a variable. it should be:[tt]
<input type=&quot;checkbox&quot; onclick=&quot;markall('abc')&quot;[/tt]

Code:
<html>
<head>

    <script language='javascript'>
    function markall(name)
    {     
      f=document.forms[0];
         
      for(x = 0; x < f.elements.length; x++)
      {
        if(f.elements[x].name == name) 
           f.elements[x].disabled = false;
      }
    }
    </script>

</head>
<body>

  <form name=&quot;myform&quot; action=&quot;test4_x.php&quot; method=&quot;post&quot;>
    <b>menu</b><br>
    <input type=&quot;checkbox&quot; onclick=&quot;markall('abc')&quot; name=&quot;def&quot; value=&quot;1&quot;>main menu<br>
    <input type=&quot;checkbox&quot; name=&quot;abc&quot; value=&quot;2&quot; disabled>submenu 1<br>
    <input type=&quot;checkbox&quot; name=&quot;abc&quot; value=&quot;3&quot; disabled>submenu 2<br>
    <input type=&quot;checkbox&quot; name=&quot;abc&quot; value=&quot;4&quot; disabled>submenu 3<br>
    <input type='submit' value='go'>
  </form>

</body>
</html>
=========================================================
while (!succeed) try();
-jeff
 
thanks for the advice! it works now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top