jquery - Read more link function on dynamically added elements not working -


i have function sets see more link on long text..it works on elements present before page load doesnt work on dynamically added elements, calling shorten() function after adding elements, works on newly added elements elements present before load , working, doesnt work....below code , can check jsfiddle here

html

    <div class="main">       <p class="readmore">this text text text text text text text text text text text text text text text text text text text text</p>     </div>     <div class="new-elem"></div>     <a href="#" class="addelem">add</a> 

js

jquery.fn.shorten = function (settings) { var config = {     showchars: 100,     ellipsestext: "...",     moretext: "see more",     lesstext: "see less" };  if (settings) {     jquery.extend(config, settings); }  jquery('body').on('click', '.morelink', function () {     var = jquery(this);     if (his.hasclass('less')) {         his.removeclass('less');         his.html(config.moretext);     } else {         his.addclass('less');         his.html(config.lesstext);     }     his.parent().prev().toggle();     his.prev().toggle();      return false; });  return this.each(function () {     var = jquery(this);      var content = his.html();     if (content.length > config.showchars) {         var c = content.substr(0, config.showchars);         var h = content.substr(config.showchars, content.length - config.showchars);         var html = c + '<span class="moreellipses">' + config.ellipsestext + '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="javascript://nop/" class="morelink">' + config.moretext + '</a></span>';         his.html(html);         jquery(".morecontent span").hide();     } }); }  jquery('.readmore').shorten();  //for load time  jquery(document).on('click', '.addelem', function () {     jquery('.new-elem').append('<p class="readmore">this text text text text text text text text text text text</p>');     jquery('.readmore').shorten();  //for newly added elements   }); 

it seems whenever "add" link clicked, running "shorten" function on of .readmore elements, attaching click handler body more once.

the first time "add" link clicked, paragraph added, , click handler added 2 times (once each of 2 paragraphs), , clicking "see more" link causes handler run 2 times. 2nd time click handler clicked, paragraph added, , click handler added 3 more times, , clicking "see more" link causes handler run 5 times. next time "add" link clicked, paragraph added, , click handler added body 4 more times. clicking "see more" link causes handler run 9 times. , on.

i think jquery('body').on('click'...) call should moved out of shorten function. also, jquery('.readmore').shorten(); should maybe changed jquery('.addelem .readmore:last').shorten();. cause click handler "see more" link attached once, , cause shorten method called once each of .readmore links.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -