Show/Hide text JavaScript error -
this example show/hide text http://codepen.io/svinkle/pen/aqwdu intend adapt in website , how fix code not repeat first lines of paragraph? in example text begins "lorem ipsum dolor sit amet, consectetur adipiscing elit. vestibulum facilisnim wanted molestie", has been repeated before 3 points ( ... ) how fix it?
// select text areas var textarea = document.queryselectorall('[data-js=content]'), maxtext = 100; // each one... [].foreach.call( textarea, function( el ) { var textarealength = el.innerhtml.length, teasertext = el.innerhtml.substr(0, 100), fulltext = el.innerhtml, showteaser = false; // check see if text length more // max if (textarealength >= maxtext) { // set flag showteaser = true; // set teaser text el.innerhtml = teasertext; el.innerhtml += el.innerhtml + '...'; // create button var button = document.createelement('button'); button.innerhtml = 'show more'; button.classlist.add('button'); el.appendchild(button); // button click event button.onclick = function () { if (showteaser === true) { // update flag showteaser = false; // update button text this.innerhtml = 'show less'; // show full text el.innerhtml = fulltext; // re-append button el.appendchild(this); } else { // update flag showteaser = true; // update button text this.innerhtml = 'show more'; // show teaser text el.innerhtml = teasertext; el.innerhtml += el.innerhtml + '...'; // re-append button el.appendchild(this); } return false; }; } else { // show full text el.innerhtml = fulltext; } });
el.innerhtml += el.innerhtml + '...';
the error in above line.you're adding el.innerhtml
twice. @ first adding ...
, adding because of shorthand +=
operator.
it should just
el.innerhtml += '...';
it's present in multiple places, may need edit those.
Comments
Post a Comment