Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically set a checkbox replacing div to "checked"
    primarykey
    data
    text
    <p>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:</p> <pre><code>$("#checkbox_"+div_id).each(function(){ $(this).hide().after("&lt;div class='class_checkbox' id='visitor_entered_"+div_id+"' /&gt;"); }); $("#visitor_entered_"+div_id).on('click',function(){ $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked')); }); </code></pre> <p>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:</p> <pre><code>if(visitor_entered==1) { alert("1, true!"); $("#visitor_entered_"+div_id).prop('checked',$(this).is('.checked')); } </code></pre> <p>It did the alert when was needed, but didn't set the div to checked. So how can I do that?</p> <p><em><strong></em>**EDIT**</strong></p> <p>The entire function:</p> <pre><code>function create_inner_divs(div_id, visitor_entered){ for(var i=0; i&lt;6; i++){ divs_array[i] = div_id*10+i; var div_width = 704/6; $("&lt;div id='"+divs_array[i]+"' style='width:"+div_width+ "px; float:right; word-wrap:normal; word-break:break-all; padding:0 2px 0 2px;'&gt;&amp;nbsp;&lt;/div&gt;").appendTo("#div_visitor_"+div_id); } $("&lt;div id='div_visitor_entered_"+div_id+"' class='div_visitor_entered'&gt;&lt;input name='checkbox_visitor_entered' type='checkbox' id='checkbox_"+div_id+"' class='input_class_checkbox'&gt;&lt;/div&gt;"). appendTo("#div_visitor_"+div_id); $("&lt;div id='div_print_voucher_icon' class='div_print_voucher_icon'&gt;&lt;/div&gt;"). appendTo("#div_visitor_"+div_id); $("&lt;div id='delete_visitor' class='delete_visitor'&gt;&lt;/div&gt;"). appendTo("#div_visitor_"+div_id); $("#checkbox_"+div_id).each(function(){ $(this).hide().after("&lt;div class='class_checkbox' id='visitor_entered_"+div_id+"' /&gt;"); }); $("#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')); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload