Demsaas Demsaas - 1 year ago 76
HTML Question

Find word in a text and make founded words in a text bold and color:red

Cant get how to change style of founded word,i know two ways how to find,like with indexof and regex but how to change style with founded index or etc?So if shortly i want to do smthing like that:
words to find:
aaa bbb osq osqqjweqj kkw

var text = $("#text").val();
var words = $("#words").val();
var arrword = words.split(',');
url: "/Demo2/Fwords/",
data: data = { "text": text, "words": words },
method: 'POST',
dataType: 'json',
success: function (data) {
$('#text').val(' ');
$('#words').val(' ');
setTimeout("$('.loader').hide()", 3000);
setTimeout("$('#lback-words').hide()", 3000);
setTimeout("$('#lback-text').hide()", 3000);
var sub_ul = $('<ul/>');
$.each(data, function (index, value) {
var sub_li = $('<li/>');
$(sub_li).html(value.w + "-" + value.q);
var appendContent = $("<section class='searchresult'><h4>Result:</h4><div class='words'>Words:</div><div class='text'>Text:<br>" + text + "</div></section>");
error: function (data) {

Answer Source

Try this:

var html = $('.your_text_element').html();
html  = html.replace('osq', '<b>osq</b>');


// get values of text and words
var text = $("#text").val();
var words = $("#words").val();
// check if text contains words
var text_after_check = text.replace( words, '<b>' + words + '</b>');
if( text != text_after_check ){
    // words found
    // add html with hightlighted text to div check_result
    // hide textarea (optional)

In yout html add the div with id = check_result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download