Results 1 to 2 of 2

Thread: javascript array disable checkbox

  1. #1
    Join Date
    May 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default javascript array disable checkbox

    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

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tedc83
    Code:
    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:

    Code:
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •