I have some DIVs inserted by jQuery.
The structure is like this:
<div class="preview_images" id="preview_upload">
<div class="image_box" id="img_box8793">
<p>
<img class="count_19" src="[...]">
<br>
<span class="del_img">löschen</span>
</p>
</div>
</div>
I want to use the on() function, before I used live().
This is my jQuery function:
$('#preview_upload div p').on("click", "span", function () {
alert('###');
parent_div_id = $(this).parent().parent('div').attr('id');
$(this).parent().parent('div').fadeOut('slow');
$('#final_img_prev #' + parent_div_id).remove();
del_val = $(this).attr('title');
$('#customfields-tf-150-tf,#customfields-tf-16-tf, #customfields-tf-17-tf, #customfields-tf-18-tf, #customfields-tf-19-tf').each(function () {
if ($(this).val() == del_val) {
$(this).val('');
var img_count = jQuery.cookie('img_count');
new_img_count = parseInt(img_count) - 1;
if (new_img_count > 0) {
jQuery.cookie('img_count', '', {
expires: -1
});
jQuery.cookie('img_count', new_img_count, {
expires: 1
});
if (new_img_count < 4) {
new_file = '<input type="file" id="file1" name="file1">';
$('#uploadForm .file_box').html('');
$('#uploadForm .file_box').html(new_file);
$('#uploadForm').fadeIn();
}
}
return false;
}
});
});
The alert() is not getting fired.
Where is the error?
EDIT:
This is my code after your answers:
jQuery(function($){
$(document).ready(function() {
$('#preview_upload div p').on("click", "span", function(){
alert('###');
});
});
)};
I am using Jquery 1.7.2. Firebug shows no errros. No alert() is fired.
Maybe it is, because the DIVs and their contents are created dynamically?
When
onis used the event should be delegated from one of static parents of the element, the div#preview_uploadis generated dynamically, so you should select another element like thebodyordocumentobject for delegating the event, try this: