display alert when mouse hovers over word in text

Posted by user1672790 on Stack Overflow See other posts from Stack Overflow or by user1672790
Published on 2012-09-15T03:18:52Z Indexed on 2012/09/15 3:37 UTC
Read the original article Hit count: 93

Filed under:
|

I have been struggling with this for a few days. I need somebody to steer me in the right direction. I have been searching on the web. I am not sure if I took the right approach. What I need is that each time a person hovers over a particular keyword, it should display an alert box. In this example the word is else. When I run the code it does not give any errors and does not display anything when mouse hovers on the word.

function on_func2()
{
    var searchString = 'else';
    var elements = document.getElementById('paragraph2');
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].innerHTML.indexOf(searchString) !== -1) 
        {
            alert('Match');
            break;
        }
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html5