Replace every word with tag

Posted by Sherif elKhatib on Stack Overflow See other posts from Stack Overflow or by Sherif elKhatib
Published on 2012-06-10T16:13:44Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 172

Filed under:
|
|

JAVASCRIPT or JAVA solution needed

The solution I am looking for could use java or javascript. I have the html code in a string so I could manipulate it before using it with java or afterwards with javascript.

problem

Anyway, I have to wrap each word with a tag. For example:

<html> ... >
Hello every one, cheers
< ... </html>

should be changed to

<html> ... >
<word>Hello</word> <word>every</word> <word>one</word>, <word>cheers</word>
< ... </html>

JAVASCRIPT hint

I am using the following code to highlight a word; however, this will highlight the whole text belonging to a certain tag. When each word is a tag, this will work to some extent. If there is a substitute that will allow me to highlight a word at a certain position, it would also be a solution.

function highlightElementAtPoint(xOrdinate, yOrdinate) {
    var theElement = document.elementFromPoint(xOrdinate, yOrdinate);
    selectedElement = theElement;
    theElement.style.backgroundColor = "yellow";
    var theName = theElement.nodeName;
    var theArray = document.getElementsByTagName(theName);
    var theIndex = -1;
    for (i = 0; i < theArray.length; i++) {
        if (theArray[i] == theElement) {
            theIndex = i;
        }
    }
    window.androidselection.selected(theElement.innerHTML);
    return theName + " " + theIndex;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about JavaScript