jquery - Existing functionality not working after dynamically loading the same dom element -


i have multiple rows, inside rows there functionality increase decrease values, checkbox, price etc..based on price sorting rows. generating after creating row internal functionality not working.

html code(clicking on private):

var categorysort = ["0 - 25", "25 - 50", "50 - 150", "150 , over"]; 
/images/up_arrow.png"> category

jquery code:

/* sort by:- private start */    $('.sort-by .private').on('click', function(){     var items = $(".rendereditem");     var privatestatus = "private";     $(".categorystatus").html("");           if(privatestatus = "private"){         var itemmaindiv = "<div style='' aria-expanded='true' id='categorylist0' class='panel-collapse collapse privatediv' />" ;     }     var collapsiblepanel = $( "<div class='collapsiblepanel collapsibleparentdiv' style='display:none'/>" ).append(         $( "<div class='panel panel-default' />" ).append(             $( "<div class='panel-heading' />").append(                 $('<div class="panel-title" />').append(                     $('<div class="pull-left categoryblock" />').append(                         $('<span class="categorytext" />').text(privatestatus),                         $('<span class="link visible-xs">').append(                             $('<a href="#" />').text('view checklist')                         )                                                    ),                     $('<span class="collapseexpand pull-right">').append(                         $('<a class="collapsed" data-target="#categorylist0" data-toggle="collapse" href="#" aria-expanded="true"/>')                     )                 )             ),             $(itemmaindiv)         )     );     $(".categorystatus").append(collapsiblepanel);        items.each(function(index) {         item = $(this);         itemtype = item.attr("itemtype");          if(itemtype == "private") {             $(".privatediv").append(item);                       }     });           var collpnldivs = $('.collapsiblepanel');      collpnldivs.each(function( index ) {         if($(this).find('.rendereditem').length > 0){                        $(this).show();         }                });      });  /* sort by:- private end */ 

this because when dynamically load content, inserting page after have installed event handlers. thus, there no event handlers on dynamic content.

the usual solutions to:

  1. install event handlers after load dynamic content.

or

  1. switch delegated event handling work dynamically created content.

in delegated event handling, bind event parent exists @ time install event handlers (even though child content not yet exist) , use event bubbling see events happen in child objects later dynamically loaded. jquery makes quite easy if use correct form of .on().

you can read more delegated event handling in these references:

jquery .live() vs .on() method adding click event after loading dynamic html

does jquery.on() work elements added after event handler created?

jquery event handlers - what's "best" method


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -