I'm am making a "word-checker" with a contenteditable
div while using mark.js to highlingt certain words/word groups.
The problem I am having is that I can't find a way to "run" Mark.js as the words are being typed (copy/pasted) in.
I have tried to achieve this by adding an event listener to the textarea, but that throws an error: mark is not defined. textarea.addEventListener("input", mark);
and by changing mark to instance textarea.addEventListener("input", instance);
which gives me an error: TypeError: Property 'handleEvent' is not callable.. Here is the fiddle.
I have also looked at various examples found on SO and on google, but I just can't seem to find what I'm looking for. The closest thing I found was this, but I still couldn't get it to work.
Any help would be much appreciated. If I can provide any additional information, please let me know.
var textarea = document.getElementById("textarea");
var instance = new Mark("div.textarea");
var red = ["red", "blood"];
var brown = ["brown", "pudding"];
var green = ["green", "grass"];
instance.mark(red, {
seperateWordSearch: false,
className: "red",
"accuracy": {
"value": "exactly",
"limiters": [",", ".", "!", "?"]
}
});
instance.mark(brown, {
seperateWordSearch: false,
className: "brown",
"accuracy": {
"value": "exactly",
"limiters": [",", ".", "!", "?"]
}
});
instance.mark(green, {
seperateWordSearch: false,
className: "green",
"accuracy": {
"value": "exactly",
"limiters": [",", ".", "!", "?"]
}
});
textarea.addEventListener("input", mark);
.red {
background-color: lightcoral;
}
.green{
background-color: lightgreen;
}
.brown{
background-color: burlywood;
}
<h2>Text checker</h2>
<div class="textarea" id="textarea" contenteditable="true">
I hate blood pudding and so do you.
</div>