Javascript code is not working

I just started to learn javascript and I am trying to write a Tooltip on Javascript.
In Html file I wrote:

< div id="tooltip">
   This is a tooltip.
< /div>

And in js file like this:

function show()
{
    var tooltip = document.getElementById('tooltip');
    tooltip.style.top = event.clientY;
    tooltip.style.left = event.clientX + 10;
    tooltip.style.display = 'block';
}
 
function hide(){
    var tooltip = document.getElementById('tooltip');
    tooltip.style.display = 'none';
}

But it doesn’t work in browser. Tell me please, where is an error?

1 Like

These both work, but you need to trigger the functions somehow.

Are you following some kind of tutorial? I really suggest starting with some sort of instruction of some sort. Otherwise you’re just fighting an uphill battle you’re probably not going to win.

For example:

There are plenty of other introductory courses out there as well.

I am also trying to improve my JavaScript skills and don’t totally understand your code. I have a question for you (sometimes if you can explain to someone else what your code is supposed to do, it helps you find the problem): How are the functions supposed to be triggered?

In most cases (i.e. syntax but not always logic) errors will display in your browser’s developer tools console.

Are any messages showing there?

There wouldn’t be in this case as the functions are not running because there is no code in the JavaScript to tell them to run.

Once the extra JavaScript is added to detect whatever event is supposed to be triggering the show and hide of the tooltip then the functions that are there should work.

There is insufficient information provided to determine what event to check for.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.