Skip to content
Skip to search - Accesskey = s
parentNode.org
The building blocks of a solid frontend.
Pasted
by
fatbrain
on
2007-02-08 18:54
as
JavaScript
Create New
http://pastebin.parentnode.org/
Links this one
http://pastebin.parentnode.org/2867
Comment
Here you go rats.
var trunc_len = 100; function truncateMe(p) { if (p) { var trunc = p.innerHTML; if (trunc.length > trunc_len) { /* Truncate the content of the P, then go back to the end of the previous word to ensure that we don't truncate in the middle of a word */ trunc = trunc.substring(0, trunc_len); trunc = trunc.replace(/\w+$/, ''); /* Add an ellipses to the end and make it a link that expands the paragraph back to its original size */ trunc += '<a href="#" ' + 'onclick="this.parentNode.innerHTML=' + 'unescape(\''+escape(p.innerHTML)+'\');return false;">' + '...<\/a>'; p.innerHTML = trunc; } }} truncateMe(document.getElementById('id_of_p1')); truncateMe(document.getElementById('id_of_p2')); If you want to truncate all <p> you can do. window.onload = function() { var ps = document.getElementsByTagName('P'); for(var i = 0; i < ps.length; i++) truncateMe(ps[i]); } // Cheers