Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight substring in element

What is the best way to highlight a specific substring in a div element in an event? By highlight, I mean apply some CSS style, like yellow background or something.

Basically I need a simple JS client side function of the form:

function (element, start, end) { 
  // element is the element to manipulate.  a div or span will do
  // start and end are the start and end positions within the text of that
  // element where highlighting should be.
  // do the stuff
}

Only one highlight will be active.

like image 602
Ayman Avatar asked Jan 29 '12 06:01

Ayman


People also ask

How do I highlight a string?

The string is highlighted by using HTML tags. The colors used for syntax highlighting can be set in the php. ini file or with the ini_set() function.

How do you highlight part of text in HTML?

The <mark> tag in HTML is used to define the marked text. It is used to highlight the part of the text in a paragraph.


2 Answers

You will need to wrap the text that you want to in it's own <span> tag so you can give that text its own style. Using your requested function definition, you could do it like this:

function (element, start, end) { 
    var str = element.innerHTML;
    str = str.substr(0, start) +
        '<span class="hilite">' + 
        str.substr(start, end - start + 1) +
        '</span>' +
        str.substr(end + 1);
    element.innerHTML = str;
}

You can then define CSS for the class hilite to control the style of that text.

.hilite {color: yellow;}

This assumes that start and end are indexes into the innerHTML of the first and last characters that you want highlighted.

If you want to be able to call it repeatedly on the same element (to move the higlight around), you could do it like this:

function (element, start, end) {
    var item = $(element);
    var str = item.data("origHTML");
    if (!str) {
        str = item.html();
        item.data("origHTML", str);
    }
    str = str.substr(0, start) +
        '<span class="hilite">' + 
        str.substr(start, end - start + 1) +
        '</span>' +
        str.substr(end + 1);
    item.html(str);
}
like image 66
jfriend00 Avatar answered Sep 19 '22 15:09

jfriend00


<div id="myElement">Hello everyone! guess Who</div>

<script>    
HTMLElement.prototype.mark=function(Str=null){var T=this;
    if(Str === null){ 
        if(T.oV){ T.innerHTML=T.oV }
        return T
    }
    var V = T.oV = T.oV || T.innerText, s = V.indexOf(Str), e = Str.length + s;
    if(s < 0){return;}//substring not found
    V = V.substr(0,s) + `<span style="background-color:lightblue">` + V.substr(s,e - s + 1) + `</span>` + V.substr(e+1);
    T.innerHTML=V;
    return T;
}

var E = document.getElementById("myElement");
  E.mark("Hello");//finds the string and highlight
  // E.mark();// revert to original text
</script>

working example:

https://jsfiddle.net/xwyqth6k/

like image 21
Saif Avatar answered Sep 21 '22 15:09

Saif