Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top