Assuming you are in control of the controlled state of your flags ...
Demo: http://jsfiddle.net/zgrRd/5/
In other words, any state in which the flags are located will be evaluated when the page loads, therefore, if the check box is not selected, the corresponding element will begin to hide. Therefore, if you set a value on the server side that checks the checkbox, this client-side JavaScript must evaluate it correctly.
Javascript
function evaluate(){ var item = $(this); var relatedItem = $("#" + item.attr("data-related-item")).parent(); if(item.is(":checked")){ relatedItem.fadeIn(); }else{ relatedItem.fadeOut(); } } $('input[type="checkbox"]').click(evaluate).each(evaluate);
HTML
<input type="checkbox" data-related-item="title1"> <span class="caption">Title</span> <div class="hidden"> <h2>Title</h2> <input type="text" id="title1"> <span class="caption">Lorem</span> </div> <hr> <input type="checkbox" data-related-item="title2" checked> <span class="caption">Title</span> <div class="hidden"> <h2>Title</h2> <input type="text" id="title2"> <span class="caption">Lorem</span> </div>
Note the use of data-* attributes. This avoids the use of the name attribute of one field to indicate a relationship to another field. You can legally name these attributes as you wish if they are prefixed with data- .
source share