3

I have check box in the form with the following values:

<input type="checkbox" name="role" value="Admin1" />Admin1<br />
<input type="checkbox" name="role" value="Admin2" />Admin2<br />
<input type="checkbox" name="role" value="Admin3" />Admin3<br />
<input type="checkbox" name="role" value="Admin4" />Admin4<br />

I have a Roles List that is coming from the server side with the values :

Roles
{
Admin1
Admin4
}

Now based on the Roles value the options Admin1 and Admin4 will be checked in the check box. How can I do this using jQuery?

2
  • 2
    How is the data being returned from the server? Please post the actual value instead of a pseudocode array. Commented Jan 7, 2012 at 2:28
  • The data is coming from a asp.net side. The Roles object is a string array looking like : { [0] = "Admin1" , [1] = "Admin4" } Commented Jan 7, 2012 at 2:59

3 Answers 3

4
$('input[value="Admin3"]').prop("checked", true);
Sign up to request clarification or add additional context in comments.

1 Comment

I think I have to use a each loop on the client side . You cannot hard code like : $('input[value="Admin3"]').prop("checked", true);
1

Maybe This can help You :

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    var peran = new Array("Admin1", "Admin4");
        $(document).ready(function() {
              $(".ceks").each(function(){
                var attrib = $(this).attr("value");
                for(var x=0; x<peran.length; x++){
                    if( peran[x] == attrib ){
                        $(this).attr("checked", "checked");
                    }
                }
              });
        });
    </script>
</head>
<body>

<input type="checkbox" name="role" value="Admin1" class="ceks" />Admin1<br />
<input type="checkbox" name="role" value="Admin2" class="ceks" />Admin2<br />
<input type="checkbox" name="role" value="Admin3" class="ceks" />Admin3<br />
<input type="checkbox" name="role" value="Admin4" class="ceks" />Admin4<br />

</body>
</html>

1 Comment

Yes thanks.. Though I saw your post late but this is the way I did it .
0

If the page is an asp.net page then why not just set the checkboxes in the code behind?

IF (criteria is met)
    (checkboxID).selected = true
end if

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.