1

Code :

var str = '2,4';
var accept_message = str.split(',');


var accept_message_html = '';
var accpet_message_Obj = {
    1: 'Friends',
    2: 'Models',
    3: 'Premiums',
    4: 'Basics'
};
$.each(accpet_message_Obj, function(key, value) {
    accept_message_html += '<span style="padding-right:30px;"><input style="float:none;" type="checkbox" name="privacy_options[]"' + (key == accept_message[key] ? ' checked="checked"' : '') + ' value="' + key + '" /> ' + value + '</span>\n';
});

$('#content-area').html(accept_message_html);

Above coding not working. I need to checkbox checked which values are 2 and 4 but there is no checkbox selected. :(

JSFIDDLE

1

1 Answer 1

3

You need use indexOf() to check if key exists in accept_message

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Code

accept_message.indexOf(key) > -1

DEMO

However, as indexOf() is supported in IE9+, You can use $.inArray()

Search for a specified value within an array and return its index (or -1 if not found).

Code

$.inArray(key, accept_message) > -1
Sign up to request clarification or add additional context in comments.

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.