views:

46

answers:

3

Hello everybody, I'm trying to disable/enable multiple form elements of an HTML page using javascript. I have a form with many fields, each row of the form has a checkbox to enable/disable the elements on the row.

The problem is, only the first two form elements get disabled (no matter the order). After that, the remaining javascript code in the function just won't be executed anymore.

Here's the code (it's part of a function called by the onChange attribute of every checkbox):

document.getElementsByName(prefix + "fase" + phaseNumber + "_" + objectNumber + "_quantity")[0].disabled = !theBox.checked;
document.getElementsByName(prefix + "fase" + phaseNumber + "_" + objectNumber + "_description")[0].disabled = !theBox.checked;
document.getElementsByName(prefix + "fase" + phaseNumber + "_" + objectNumber + "_price")[0].disabled = !theBox.checked;
document.getElementsByName(prefix + "fase" + phaseNumber + "_" + objectNumber + "_language")[0].disabled = !theBox.checked;

Each form element has a different (and unique) name, and the single lines of code work just fine... until you put them together. For example: in the above code, "quantity" and "description" fields will get disabled/enabled, but "price" and "language" won't.

If i change the order of the lines, the first two get always executed, no matter what. Then every line of code doesn't work; it's like it's commented. I even put some alerts to try debugging, but they just get ignored (no dialog shows up at all) if I insert them after the above code. The elements names are correct.

I'm sure I've made a mistake somewhere, but since the single lines of code are working, I don't know where to look... it's driving me nuts!

Please, I could really use some help.

A: 

It is difficult to guess the true cause of your problem because it depends on the content of HTML this Javascript is supposed to work on and you have not included that.

But I do have a suggestion: avoid getElementsByName(). According to the specification of HTML, multiple elements can have the same name and if you are dealing with unique elements, using the id="uniqeintentifier" attribute along with getElementById() is a better idea and it requires less code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Form disabling sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script  type="text/javascript">
 function toggle() {
  var prefix = 'a', phaseNumber = 1, objectNumber = 1;
  var theBox = document.getElementById('theBox');
  document.getElementById(prefix + "fase" + phaseNumber + "_" + objectNumber + "_quantity").disabled = !theBox.checked;
  document.getElementById(prefix + "fase" + phaseNumber + "_" + objectNumber + "_description").disabled = !theBox.checked;
  document.getElementById(prefix + "fase" + phaseNumber + "_" + objectNumber + "_price").disabled = !theBox.checked;
  document.getElementById(prefix + "fase" + phaseNumber + "_" + objectNumber + "_language").disabled = !theBox.checked;
 }
</script>
<form action="#">
 <input id="theBox" type="checkbox" checked="checked" onclick="toggle();" />
 <input id="afase1_1_quantity" type="text" />
 <input id="afase1_1_description" type="text" />
 <input id="afase1_1_price" type="text" />
 <input id="afase1_1_language" type="text" />
</form>
</body>
</html>
Saul
Thank you for your time and your advice. I noticed this problem already, as the project grew bigger and bigger, but I was too lazy to change it ^^ anyway, now I corrected the code, using IDs in the HTML source and getElementById(), but the problem remains.
Valerio
A: 

Why don't you try with jQuery? it's much easier interact with multiple elements using jQuery than classic javascript. take a look here: http://it-things.com/index.php/2010/09/using-jquery-to-interact-with-multiple-items/

Joao Pinto
Altough this problem was just a mistake of mine, I'll definitely take it into consideration for my next project... thank you.
Valerio
A: 

I just made it work and, as I thought, it was a very bad mistake.

The problem was, not every form row in the HTML has all of those 4 fields. So, basically, I was trying to access a property of a null object.

I solved it by putting:

if( document.getElementById(id) != null )

before trying to manipulate the element.

Sometimes when you're in a hurry, you end up neglecting some very basic stuff...

Thank you for your time.

Valerio
When asking questions like this, it is usually a good idea to include HTML. Looking only at the Javascript there is no way to know for certain where the bug is.
Saul
You're right. Next time I'll post HTML, too. I hope there isn't a next time, though ;)
Valerio