Reputation: 13
I have successfully used below script to get predefined text highlighted in the textarea. http://www.strangeplanet.fr/work/jquery-highlighttextarea/
How it is possible to get the predefined text to be changed on the fly? E.g. user write "green" in specific input box and then "green-words" are highlighted in textarea?
Upvotes: 0
Views: 1966
Reputation: 7954
$("#input-id").bind('keypress',function () {
var word = $(this).val();
if (word) {
$("textarea").highlightTextarea({
words: [word]
});
}
});
Upvotes: 0
Reputation: 1236
$("#input-id").keydown (function () {
var word = $(this).val();
if (word) {
word += '-words';
$("textarea").highlightTextarea({
words: [word]
});
}
});
Upvotes: 0
Reputation: 4830
Hook into the keydown event of the input box and change the words array.
I have never used the jQuery plugin you linked to but something like this should work:
$("#target").keydown (function () {
$("textarea").highlightTextarea({
words: [$(this).val()]
});
});
Upvotes: 0
Reputation: 123377
Maybe you could try something like
<input type="text" id="wordtohighlight">
<textarea>lorem ipsum green sit dolor amet green</textarea>
Js/jQuery
$('#wordtohighlight').on('change', function() {
var v = $(this).val();
$("textarea").highlightTextarea({
words: [v]
});
}
With this code snippet you call the plugin when input changes (e.g. when input text
loses focus/you click elsewhere on the page) . If you prefer you could try to use keyup
event
Upvotes: 1