javascript - Insert div into random location inside a container of divs -
how insert div random location inside container of divs? similar insert div in random location in list of divs except i'm not using list.
right appears inside random container div, not randomly inside container itself:
http://jsfiddle.net/frank_o/qxdl3/15/
html:
<div class="template" style="display: none;"> <div class="image"> <img src="http://placekitten.com/75/150"> </div> </div> <div class="main"> <div class="image"> <img src="http://lorempixel.com/75/150"> </div> <div class="image"> <img src="http://lorempixel.com/75/150"> </div> <div class="image"> <img src="http://lorempixel.com/75/150"> </div> </div>
js:
var template = $('.template').find('.image').clone(), target = $('.main'), targetchildren = target.find('.image'), random = math.floor(math.random() * targetchildren.length); targetchildren.eq(random).append(template);
you close!
instead of .clone()
, try using .html()
example below.
also, changed loop along random number generator.
works now. refresh page see 3 random kitty photos added somewhere within other images.
Comments
Post a Comment