Zander
Zander

Reputation: 2684

Wrap words with spans after entering text into textarea

My scenario is similar to those live commenting tutorials we've all seen: type text into a textarea & have it display somewhere else on the page while you type. I have the live text input sorted using David Walsh's jQuery tutorial at http://davidwalsh.name/jquery-comment-preview .

Is there a way for me to wrap each word with a span tag in the ouputted div area as well?

You can see my progress at: http://jsfiddle.net/mrmartineau/epdwm/

P.S. it would be lovely to output the text using p tags instead of one long line of text with line breaks, like the question entry form here does.

Many thanks in advance.

Upvotes: 0

Views: 1923

Answers (2)

Andy
Andy

Reputation: 30135

something like this? http://jsfiddle.net/epdwm/4/


updated the fiddle: http://jsfiddle.net/epdwm/6/

the important part is the: .replace(/( |, |\. )/g,'[/span]$1[span]') if you want to add more punctuation, add them in the expression.

hope this helps

Upvotes: 2

sunn0
sunn0

Reputation: 3046

If I understand the expected result correctly (p instead of line breaks and span between around each word).

http://jsfiddle.net/epdwm/5/

Upvotes: 0

Related Questions