I am trying to record every word from a paragraph of text when it is clicked on. However, I would also like to provide visual feedback to the user when they have clicked on a word. This code has been very helpful. But I don’t seem to be understand how to add a class to it. I’ve marked the areas where my added code is not working:
var book = 'Lorem ipsum dolor sit amet, cu eum eros vitae persequeris, laudem possit nam ex. Labore eloquentiam per an. Sit ex omnesque interpretaris, habeo tantas eos ad, ea eos ludus inciderint. Facete tritani pro ei, vim evertitur liberavisse ex. Ridens indoctum duo cu, est utamur aliquando expetendis ne. Cum nusquam definiebas ex, id esse neglegentur cum, eu libris bonorum volumus vis. Ius et quis omnis graeco, no his nullam perpetua dissentiet. No vix possim scripserit consequuntur, te mnesarchum philosophia sed. Ne mea putent iudicabit, in eam ipsum viris dicunt. Eum amet accommodare ex, sint malis adversarium at qui.'
new Vue({
el: '#app',
data: { book: '' },
methods: {
doSomething(e) {
var content = e.target.textContent
var pos = window.getSelection().anchorOffset
content = content
.substring(0, content.indexOf(' ', pos))
.trim()
content = content
.substr(content.lastIndexOf(' ') + 1)
.replace(/[.,:;!?()+-]/g, '')
// not working code
.add('highlight')
console.log(content)
}
},
created() {
// load external data
this.book = book
}
})
.highlight {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<p @click="doSomething" v-html="book"></p>
</div>
I would like each word to remain highlighted when clicked on. For example, if you click on "Lorem ipsum dolor" all 3 words should be highlighted in yellow. Preferably, if clicked again, they would be unhighlighted too.
2
Answers
Split your text into words using
b
regex, make each word its own , and then highlight thosePlayground
I looked at the mentioned stackoverflow answer in your post and came up with the following solution.
spans
to each wordcolor
property ieevent.target.style.color
Note : This code was not thought by me. It was copy-pasted from the above stackoverflow link. Just modified it for the scenario 🙂
Also, I don’t know jack shit about
vue
lol