PDA

View Full Version : javascript array disable checkbox



tedc83
05-11-2005, 02:34 PM
Hi all! Im new to this forum so I will give it a shoot and post my first thread... Im trying to do something with PHP and Javascript together. I have an array in PHP which I have already successfully passed over to a Javascript array. The array comes from a table of restricted businesses.

So, what I need to do is when a user clicks on a radio button that says "no my business is not one of the following" then I need to disable all the checkboxes of the restricted businesses. (since they come from the same array, the amount of checkboxes are the same as the amount of values in the array)

So my main problem is that the checkbox names have quared brackets like this:

<input type="checkbox" name="rb[1]">

So I am calling the following function when I hit the radio

function disableNoBiz(){
var x = rbjsA;
for (var i = 0;i<x.length;i++){
document.quoteService.rb[i].disabled = true;
}
}



rbjsA is the array which contains all the restricted businesses. So I see that somehow I need to concatenate the squared brackets because it is understanding them as something else other than the name of the field.

Any ideas anyone?

Thanks!

Ted

mwinter
05-13-2005, 10:59 AM
function disableNoBiz(){
var x = rbjsA;
for (var i = 0;i<x.length;i++){
document.quoteService.rb[i&#93;.disabled = true;
}
}When square brackets are used like they are above, they are interpreted as property accessors. On each iteration, the scripting engine will be trying to find numbered properties, 0 through (x.length - 1), of a variable, rb. Instead, we need to use square brackets in a different way:


document.forms.quoteService.elements['rb[' + i + ']'].disabled = true;Notice the expression - a string concatenation - in the outer set of square brackets? The result of this concatenation (for example, rb[0] on the first loop iteration) is used to look up a form element.

Hope that helps,
Mike


P.S. Use the [code&#93;...[/code&#93; sequences to show code snippets, and use spaces (preferably two) not tabs to indent. Deep nesting becomes unmanagable at eight spaces per tab.