javascript - Calling a function inside an other function in Js -
i not understand doing wrong in can explain me ?
https://jsfiddle.net/4pvhslew/
the idea have simple function of resuming text. if text long put "..." , on click display full length. , add span @ end hide again.
so #infocountry <p>element on 200 characters.
if(document.getelementbyid("infocountry").innerhtml.length > 200){ function test(){ var fullinfo = document.getelementbyid("infocountry").innerhtml; var semiinfo = fullinfo.substring(0,200) + '<span id="plusinfo">...</span>'; document.getelementbyid("infocountry").innerhtml = semiinfo; var voirplus = document.getelementbyid("plusinfo"); voirplus.addeventlistener("click", function(){ document.getelementbyid("infocountry").innerhtml = fullinfo +'<span id="moinsinfo"> cacher</span>'; //affiche tout et "cacher" var voirmoins = document.getelementbyid("moinsinfo"); voirmoins.addeventlistener("click", test()); // here bug ? }) } test(); } it work until try recall it. console stay empty. don't understand miss understanding there :/
something work. doesn't require <span> question suggests
function showexcerpt(elem) { var text = elem.textcontent; if (text.length <= 200) return; var link = document.createelement('a'); link.href="#"; link.onclick = function(event) { event.preventdefault(); elem.textcontent = text; }; link.textcontent = '...'; elem.textcontent = text.substring(0,200); elem.appendchild(link); } showexcerpt(document.getelementsbytagname('p')[0]); showexcerpt(document.getelementsbytagname('p')[1]); <p> meatloaf tongue hamburger shank bresaola pork ribeye jerky sirloin bacon meatball venison. swine andouille turkey, porchetta pork drumstick cupim sirloin chuck meatloaf hamburger ball tip sausage frankfurter. ball tip hamburger beef ribs shankle. pig spare ribs sausage meatball beef tenderloin tail bresaola pork loin. tenderloin jowl pork chop, brisket frankfurter pork venison filet mignon. ground round flank shank, tri-tip sausage beef ribs pig tenderloin filet mignon sirloin pork leberkas. drumstick shoulder salami prosciutto andouille pork chop spare ribs turducken hamburger pork belly ham meatloaf rump alcatra.</p> <p>chicken t-bone pig flank. strip steak hamburger t-bone tri-tip</p> warning: if
<p>contains html elements within, such<b>some words</b>it's possible 200 chars split tag in half, resulting in"this text ... <b>some wo". cause additional issue you.
Comments
Post a Comment