tags:

views:

21

answers:

1

I have a div element containing some text. When the user clicks a word inside that div I'd like to highlight just that word.

In order to do this I need to know what character position in the text the click occurred at, so I can then locate nearby whitespace and insert some formatting around the word.

Finding out where the click occurred within the text is the trick here. Is that kind of thing possible?

+2  A: 

If your page is auto-generated, you might consider pre-processing the page by putting a <span class = 'word'> around every word in every selectable div. You might be able to this with javascript after the fact, and I think that would be your solution regardless, but pre-processing would make it easier.

The problem with relying on the absolute position of the word is that users can scale their fonts, which makes this task especially hard. By wrapping a span around every individual word, you can easily select which word was clicked by applying the click event to the span elements.

Stefan Kendall
That should work perfectly. I'll wrap each word server side like so:<span class='word' id='word001' onclick='doSomething(this);'> ...Thanks for quick answer.
Marc