I have a function that creates inner divs for a parent div. It receives 2 parameters: "div_id" and "visitor_entered". The function also puts a checkbox in one of these created divs and replaces it with a div with a background image. When I click on the div - it changes the background image. Here's how I do it:
$("#checkbox_"+div_id).each(function(){
$(this).hide().after("<div class='class_checkbox' id='visitor_entered_"+div_id+"' />");
});
$("#visitor_entered_"+div_id).on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'));
});
What I want now, is when this function runs, to check if the "visitor_entered" equal to 1, and if so - set that "#checkbox_"+div_id div to "checked". I tried it like that:
if(visitor_entered==1)
{
alert("1, true!");
$("#visitor_entered_"+div_id).prop('checked',$(this).is('.checked'));
}
It did the alert when was needed, but didn't set the div to checked. So how can I do that?
**EDIT**
The entire function:
function create_inner_divs(div_id, visitor_entered){
for(var i=0; i<6; i++){
divs_array[i] = div_id*10+i;
var div_width = 704/6;
$("<div id='"+divs_array[i]+"' style='width:"+div_width+
"px; float:right; word-wrap:normal; word-break:break-all; padding:0 2px 0 2px;'> </div>").appendTo("#div_visitor_"+div_id);
}
$("<div id='div_visitor_entered_"+div_id+"' class='div_visitor_entered'><input name='checkbox_visitor_entered' type='checkbox' id='checkbox_"+div_id+"' class='input_class_checkbox'></div>").
appendTo("#div_visitor_"+div_id);
$("<div id='div_print_voucher_icon' class='div_print_voucher_icon'></div>").
appendTo("#div_visitor_"+div_id);
$("<div id='delete_visitor' class='delete_visitor'></div>").
appendTo("#div_visitor_"+div_id);
$("#checkbox_"+div_id).each(function(){
$(this).hide().after("<div class='class_checkbox' id='visitor_entered_"+div_id+"' />");
});
$("#visitor_entered_"+div_id).on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'));
});
if(visitor_entered==1)
{
alert("1, true!");
$("#visitor_entered_"+div_id).prop('checked',$(this).is('.checked'));
}
}