473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Truncating long words in a larger block of HTML

Hi, everybody -- first post for me, though I land here via Google all the time. This is the first time, though, that I just haven't been able to find anything even remotely resembling a solution to my problem.

I have a large block of HTML. I won't get into unnecessary specifics, but let's just say that it's not possible for me to edit this HTML directly or on the server side. Any changes I make to it has to be on the client side, through JavaScript magic of some sort. What I want to do is grab any words in this block that are over a certain length and truncate them, maybe slap a "..." at the end. I've tried many techniques, but I just haven't been able to do it. Here's the closest I've gotten:

Expand|Select|Wrap|Line Numbers
  1. var sbText = document.getElementById('searchbody').innerHTML;
  2. var sbTextArr = sbText.split(' ');
  3.  
  4. for (var s=0; s<sbTextArr.length; s++) {
  5.     document.getElementById('searchbody').innerHTML = '';
  6.     var tempText = document.createTextNode(sbTextArr[s] + ' ');
  7.     document.getElementById('searchbody').appendChild(tempText);
  8.     if (sbTextArr[s].length > 30) { sbTextArr[s] = 'chicken'; }
  9. }
  10.  
I haven't bothered writing the whole thing yet because I haven't been able to get the first part working, nor have I decided exactly what I want to do with the words that are too long. So in the example above, let's just say I want to replace all words longer than 30 characters with the word "chicken."

I know innerHTML is proprietary, but it works and all I'm using it for here is to read the HTML and then zero it out, so I figured it was OK.

Anyway, as you can see, what I'm trying to do here is read the contents of a div called "searchbody," put those contents into a variable, create an array with each word in that variable as an element by splitting the variable at each space, replace each array element greater than 30 characters with the word "chicken," and appending all the resulting elements to the end of the searchbody div. This almost works, but the problem is that the HTML doesn't render as HTML, it renders as text. An image tag won't show up as an image -- it shows up as an image tag.

Does anyone know any way to circumvent this problem? Or, alternatively, an entirely different approach?

Thanks a lot!
Jan 16 '07 #1
2 2029
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function test() {
  3. var sbText = document.getElementById("searchbody").innerHTML;
  4. var sbTextArr = sbText.split(' ');
  5. var tempText = "";
  6. for (var s=0; s<sbTextArr.length; s++) {
  7. if (sbTextArr[s].length > 30) { sbTextArr[s] = "chicken"; }
  8. document.getElementById('searchbody').innerHTML = "";
  9. tempText = tempText + " " + sbTextArr[s];
  10. document.getElementById('searchbody').innerHTML = tempText;
  11. }
  12. }
  13. </script>
  14. <input type="button" onclick="test();" value="Test"><br>
  15. <div id="searchbody">
  16. this is a test to check word length thiswordislongerthan30adsfasdfasdf
  17. <br>
  18. so lets see what happens here
  19. </div>
  20.  
Heres a snippet I threw togethor try it out it.
Let me know if this works for you.
Jan 16 '07 #2
It does work! Thanks a million for your help.
Jan 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: cppaddict | last post by:
It struck me as odd today that while class definitions require a semi-colon at the end, function definitions do not. I was curious if this small syntactic difference represents a larger conceptual...
1
by: Daniel Déchelotte | last post by:
Hi, This is actually two questions in one: how to have a block just fit the data it contains (see my "table-cell" kludge) and how to have it centered in a larger block? My idea is to have my...
2
by: igorsl | last post by:
Hi, I have a problem to insert(update) a long text (more than 64K) into SQL 2000 (datatype - 'text'). It cuts the data and insert only 64K. MSDN says: "When the ntext, text, and image data values...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
24
by: Michael B Allen | last post by:
I use long longs occasionally. I know there are some limitations regarding the standards such as not using long long constants but what's the big deal? Why is long long not used so much? Mike
1
by: andrewcw | last post by:
Should be simple, I open fileX, then serialize my content back out ( with position set to 0 ). This works well as long as my new output is larger than the old file input. But when my output is...
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
73
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.