2

I want to pass to an array of controls' IDs to a javascript script function so it will switch control's enable state.

For example, in C# it would be like this:

func(false, new[] { "Control1", "Control2", "Control3" });

In that function I want to find corresponding controls and disable/enable them. For one control I do this next way:

<script type="text/javascript" language="javascript">
    function switchControls(value, arr) {
        for (var n = 0; n < array.length; n++)
            document.getElementById([n]).disabled = value;
    }
</script>

<asp:CheckBox runat="server"
     onclick="switchControls(this.checked,
        [
            '<%= Control1.ClientID %>',
            '<%= Control2.ClientID %>'
        ])" 
     Text="Take?" />

How to implement this properly? Have I to use jQuery?

5
  • 1
    An array literal in JS is just a comma-separated list [in, square, brackets]. The weird thing is that you already have an example of this in your onclick handler code, so you already know how to do it. You are already sending two control ids to the function in an array, not one. So what's the question? Commented Apr 5, 2010 at 17:56
  • 1
    Seems like a bug in array indexing inside of switchControls. Commented Apr 5, 2010 at 17:58
  • @ajm: Still doesn't work. alert(arr.length) shows nothing Commented Apr 5, 2010 at 18:01
  • What does the call to switchControls look like? Commented Apr 5, 2010 at 18:03
  • @ajm: I answered your comment in your answer below Commented Apr 5, 2010 at 19:16

3 Answers 3

4

No jQuery (or any other library) necessary.

It looks like your code will do the trick with a modification or two:

<script type="text/javascript">
    function switchControls(value, arr) {
        for (var n = 0; n < arr.length; n++){
            document.getElementById(arr[n]).disabled = value;
        }
    }
</script>  

As long as your ASP statement passes in a Boolean and an array of IDs (it looks like it does already, but I'm not familiar with ASP), it should work. For example, your onClick could call switchControls like this:

switchControls(true, ['foo','bar','baz']);
Sign up to request clarification or add additional context in comments.

2 Comments

Neither switchControls(this.checked, [ <%= txtMinimalCommission.ClientID %> ] ) nor switchControls(this.checked, [ txtMinimalCommission.ClientID ] ) passes as an array. Of it isn't recognized as an array by JS function
clientID is not a valid property JavaScript would recognize. You would need to have ASP render that out and then pass it as a string in your array. Something like switchControls(this.checked, [ "<%= txtMinimalCommission.ClientID %>" ] ).
2

you don't "HAVE" to use jQuery, but it's somekind cooler to use it :)

function checkme(arr){
   if($.isArray(arr)){
     $.each(arr, function(){
       var currentState = this.attr('disabled');
       if(currentState == 'disabled' || currentState == 'true'){
          this.removeAttr('disabled');           
       }
       else{
          this.attr('disabled', 'disabled');
       }
     });
   }
}

usage: checkme([$("#someid"), $("#anotherid"), $("#anotherid")]);

2 Comments

Your code needs to be modified to allow for a toggle to enable/disable the controls.
How to combine ASP.NET call '<%= code %>' and jQuery call $("code") ?
0
<script type="text/javascript" language="javascript">
    function toggleControls(value) {
        $('.toggle').each(function() {
            if (value) {
                $(this).removeAttr('disabled');

            }
            else {
                $(this).attr('disabled', 'true');
            }
        });
    }
</script>

<asp:CheckBox runat="server" onclick="toggleControls(this.checked)" Text="Take?" />
<asp:TextBox runat="server" CssClass="toggle" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.