Initially when I had static elements (same class but different ids) all over my webpage, below was the jquery I was using, which worked correctly.
<script>
$(document).ready(function () {
$('.swipe').each(function () {
var pdid=$(this).attr('id');
//create a closure variable instead of a global one
var Slider = $(this).Swipe({
continuous: false,
stopPropagation: true,
callback: function (index, elem) {
lid=index+1;
},
}).data('Swipe');
//use relative tranversal to find the next/prev elements
$(this).next().next('.next').click(function (data) {
Slider.next();
$("#fimg_"+pdid).val($("#"+pdid+"_"+lid).data('src'));
// alert($("#"+pdid+"_"+lid).data('src'));
});
//try using $.proxy
$(this).next('.prev').click(function(data) {
$.proxy(Slider.prev());
$("#fimg_"+pdid).val($("#"+pdid+"_"+lid).data('src'));
});
});
$(".approveid").click(function(){
var adid=$(this).attr('id').slice(8);
alert(adid);
$("#share_rss_form_"+adid).submit();
});
$(".category").change(function() {
// Get the value of the option that was chosen
// You could also do: var currentValue = this.options[this.selectedIndex].value;
var currentValue = $(this).val();
var cid=$(this).attr('id').slice(9);
// Don't do anything if the "Please select" option was chosen
if (currentValue == '0') {
return;
}
// Load some data from the server into the next <div>
$.get('../abc_options.php?cat='+currentValue, function( data ) {
$("#selectTags"+cid).prepend(data);
$("#selectTags"+cid).trigger('liszt:updated');
});
});
$('.selectTags').chosen();
// $('#selectTags1').trigger('chosen:updated');
$('.share_rss_form').each(function(index,e1){
$(e1).validate({
errorClass: "error",
validClass: "success",
highlight:function(element, errorClass, validClass) {
$(element).parent().addClass(errorClass).removeClass(validClass);
},
unhighlight:function(element, errorClass, validClass) {
$(element).parent().addClass(validClass).removeClass(errorClass);
},
submitHandler: function(form) {
alert(form.id) ;
$(form).ajaxSubmit();
// return true;
}
});
});
$('.loadNext').click(function(){
window.location.replace("abc"+$(this).attr('id')+".htm");
});
});
</script>
However, I now need to generate those static HTML elements dynamically on the fly. It would be like using a .post to a url and fetching the entire content on click of a button. The above jquery doesn't seem to be binding to the dynamically created elements.
While I am looking into the options of jquery .live and .on, I am not sure of how to convert the above code to bind to my dynamic elements at runtime. As I comprehend, .on works for some events like "Click", but if you see the jquery code above, I dont have click events for everyone (eg: swipe,selectTags,shareRSSForm).
Could someone please help in showing me how to convert the above code to bind to dynamically created elements at run time?
UPDATED ISSUE: I have Next button coded as below
$('.loadNext').on('click',function(){
$('#loadNextVal').val(parseInt($('#loadNextVal').val())+parseInt(1));
$("#share_rss_form_load").load("RSSFeeds/abc3.htm #share_rss_form_"+$('#loadNextVal').val());
//alert(1)
runMyCode($("#share_rss_form_load").load("RSSFeeds/abc3.htm #share_rss_form_"+$('#loadNextVal').val()));
});
and the HTML code is Next
When I click the next button the dynamic content is loaded but the jquery doesnt bind to it immediately, when I click the Next button again to load the next element, for a fraction of few seconds, the jquery gets binded to the previous dynamically created element before changing to the next dynamic elemnt. It looks like a problem of late binding or binding too early before the element exists because if I place a alert box in between (as seen above), it works correctly
Please help
UDATED: my runMycode function
function runMyCode(parent){
$('.swipe',$(parent)).each(function () {
var pdid=$(this).attr('id');
//create a closure variable instead of a global one
var Slider = $(this).Swipe({
continuous: false,
stopPropagation: true,
callback: function (index, elem) {
lid=index+1;
},
}).data('Swipe');
//use relative tranversal to find the next/prev elements
$(this).next().next('.next').click(function (data) {
Slider.next();
$("#fimg_"+pdid).val($("#"+pdid+"_"+lid).data('src'));
// alert($("#"+pdid+"_"+lid).data('src'));
});
//try using $.proxy
$(this).next('.prev').click(function(data) {
$.proxy(Slider.prev());
$("#fimg_"+pdid).val($("#"+pdid+"_"+lid).data('src'));
});
});
$('.selectTags',$(parent)).chosen();
// $('#selectTags1').trigger('chosen:updated');
$('.share_rss_form',$(parent)).each(function(index,e1){
$(e1).validate({
errorClass: "error",
validClass: "success",
highlight:function(element, errorClass, validClass) {
$(element).parent().addClass(errorClass).removeClass(validClass);
},
unhighlight:function(element, errorClass, validClass) {
$(element).parent().addClass(validClass).removeClass(errorClass);
},
submitHandler: function(form) {
alert(form.id) ;
$(form).ajaxSubmit({
success: function(data) {
alert(data);
}
});
// return true;
}
});
});
}