I have one main "Div" on which after clicking it gets split into n X n matrix. On every click inside it with a random colour div. Until here it's fine, now I want to create a click function on that random colourful div which currently is on any where inside the whole main "div"..
$(window).load(function() { var no = 1, $m = $(".main_div"), size = 200; $m.live('click', function() { no++; var n = no * no, i, _size; $m.empty(); for (i = 0; i < n; i++) $m.append($('<div title=' + i + '/>')); _size = size / no; $m.find('> div').css({ width: _size, height: _size }); var colors = ["#FFFFFF", "#CC00CC", "#CC6699", "#0099CC", "#FF99FF"]; var rand = Math.floor(Math.random() * colors.length), randomTotalbox = Math.floor(Math.random() * $('.main_div div').length); $m.find("div:eq(" + randomTotalbox + ")").css("background-color", colors[rand]); var rand = Math.floor(Math.random() * colors.length); }); }); .main_div { width: 200px; height: 200px; background-color: #9F0; } .main_div > div { float: left; box-shadow: 0 0 1px #000; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main_div" id="demo"> </div> Here is a fiddle...Code
<div>" into a variable as soon as you find it, then do whatever you want with it. No rocket science.