Say, I have:
<p> This is a sentence </p>
And when a user clicks on words in the sentence (this, is, a, sentence), the web page will know that specific word is clicked. Is there something in js or the jQuery library for that?
I want to do that for my project in Django so that when they click on a word, I know which word in the sentence is clicked (not the whole sentence itself).
I only have 2 ideas for that:
- Split the sentence into different elements so that it separates words when its clicked using javascript and getElementByID
OR - Similar to #1 but I just use the anchor tag and pass the link and the value of the link in the same form so that I know which word in the sentence is passed () I’m guessing there’s something wrong with this too.
Is there any better way? Or what is the best way?
We did this once, it was a really nasty thing to do on a whole document, but for a paragraph it wasn’t bad.
Take the text node, split it on white space. Then, wrap each word in a span and put it back into the document.
Attach a listener to the PARENT NODE and use delegation to figure out which span was clicked on.