Dynamic Div not found in selector
I have dynamic divs being added to a contianer. I create them like this
- $(document).ready(function() {
- buildDiv(3, 3, 128, 128, 'smile_face.png');
-
- });
- function buildDiv(x, y,width,height, img) {
- var ele = $(document.createElement('div'));
- ele.css('top', (x * 128) + 'px');
- ele.css('left', (y * 128) + 'px');
- ele.css('width', width + 'px');
- ele.css('height', height + 'px');
- ele.css('z-index', 0);
- ele.css('background-image', 'url(\'' + img + '\')');
- ele.css('position', 'absolute');
- $('.mapContainer').append(ele);
- ele = $(document.createElement('div'));
- ele.css('top', (x * 128) + 'px');
- ele.css('left', (y * 128) + 'px');
- ele.css('width', width + 'px');
- ele.css('height', height + 'px');
- ele.css('z-index', 5000);
- ele.css('position', 'absolute');
- ele.addClass('clickable');
- ele.attr('id', toStore(x, y));
- $('.mapContainer').append(ele);
-
- }
- function toStore(x, y) {
- return (x << 8) ^ y;
- }
I think have a selector which looks like this
- $(".clickable").click(
- function() {
- alert('CLICKED');
- });
The problem is when the dynamic DIV is added to the form, it shows up but is not clickable. The event is never called. When a div is added with the class clickable on page load, the event is triggered.
Im guessing there is some way to add an element to the clickable selector but I can not figure it out.
Thank you.