views:

627

answers:

1

I need to write some client-side javascript for a webpage that will truncate the text inside a div, when the onclick event for a button is fired (the purpose of the button is so that users can toggle the displayed text between complete and truncated versions). There won't be any HTML tags in the text, but there may be HTML encoded characters such as " and &

Is there away to do this that is better than having to HTML decode the string, truncate and encode again?

This seems like it might be a fairly common task that has been done before. I'm not that experienced with JavaScript, so I don't know if there's a better way to do it with javascript than encoding/decoding.

+1  A: 

Decoding, truncating then encoding makes sense. Once decoded, the information in the string is normalized for the task.

Writing an HTML-aware string-truncating function seems like the wrong approach.

a paid nerd
I agree. I was just hoping there was some great JavaScript feature or trick to do this with because it seems like a common enough task. I this approach is simple enough that any trick is just as much or more work to implement.
Adam Porad