html - Display image from directory using jQuery? -
i'm learning basics of web development, 1 thing haven't encountered solution yet getting image directory using jquery. if have checkbox defined follows...
<input id="check" type="checkbox">
... how can image directory, , display in <div>
based on current state of checkbox (without messing html)? i've found far this, showing existing contents of <div>
, correct?
$(document).ready(function(){ $('#check').click(function(){ if(!$(this).is(':checked')){ ("div").show(); } }); });
the reason don't want touch html want gain more in-depth understanding of more powerful functions of jquery (i don't want build bad habits), proving difficult thing on own. i'd appreciate (or hints)!
tl;dr: checked box = image, unchecked = no image. jquery because of reasons.
you can(assuming div present)
$(document).ready(function () { //create reference div can reused later var $div = $('div'), $img; $('#check').change(function () { //set display status of div based on checked state of checkbox $div.toggle(this.checked); if (this.checked) { //if checked , img not present create 1 if (!$img) { $img = $('<img />', { src: '//placehold.it/64/00ff00' }).appendto($div); } } }); });
demo: fiddle
Comments
Post a Comment