0

I have the following piece of code :

for(var i = 0, len = data.UserDetail.RoleNames.length; i < len; ++i) {
    var rolevalue = data.UserDetail.RoleNames[i];
    //alert(rolevalue)

    $('input[value = rolevalue]').attr('checked', true); // I am getting a exception in this line !! :(
}

I am not able to check the values which are already present in the checkbox. The checkbox is having the following value

<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 />

What will be syntax? I have also tried using the prop() function as well but it does not work.

2 Answers 2

3

It seems a bit of an odd way to do things, but I'm guessing what you want is:

$('input[value=' + rolevalue + ']').attr('checked', true)

I'm not 100% sure that the selector works this way, but if it does, that code will be better than yours!

Sign up to request clarification or add additional context in comments.

1 Comment

Yipeeee it worked after I made minor modification :$('input[value="' + rolevalue + '"]').attr('checked', true)
0

This will work too:

$('input[value='" + rolevalue + "']').is(':checked');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.