...

View Full Version : force long strings to wrap...how?



hothousegraphix
04-20-2007, 11:07 PM
Would anyone know of a way to force long strings to wrap...say at 40 characters using JavaScript?

Say is I have a reoccurring class labeled class="location" I can get to each item with that following -

//getElementsByClass Written By: Dustin Diaz
//http://www.dustindiaz.com/getelementsbyclass/

//Slight minor modification by: Jon Christopher
//http://www.MondayByNoon.com */

function getElementsByClass(node,searchClass,tag) {
var classElements = new Array();
var els = node.getElementsByTagName(tag); // use "*" for all elements
var elsLen = els.length;
var pattern = new RegExp("\\b"+searchClass+"\\b");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}

function tree(num){
var myEls = getElementsByClass(document,'location,'p');
// find string count and if over 40 insert <br />
// ???????????????????????????????????????
}

Or would this be better done server side?

rwedge
04-21-2007, 03:28 AM
You can use the substring method:
// assumes less than 80
if (myEls.length > 40) {
str1 = myEls.substring(0,40);
str2 = myEls.substring(40,myEls.length);
alert(str1+'<br />'+str2);
}

felgall
04-21-2007, 03:59 AM
http://javascript.about.com/library/blspline.htm has a script to wrap text at a specified number of characters that only breaks words that are longer than the specified length. All other new lines will be inserted between words.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum