SO - 29089838
by Arun P Johny
HTML
<input id="check" type="checkbox" />
<div></div>
JavaScript
$(document).ready(function () {
//create a reference to the div which can be reused later
var $div = $('div'),
$img;
$('#check').change(function () {
//set the display status of the div based on checked state of the checkbox
$div.toggle(this.checked);
if (this.checked) {
//if it is checked and the img is not present create one
if (!$img) {
$img = $('<img />', {
src: '//placehold.it/64/00ff00'
}).appendTo($div);
}
}
});
});