2

I have a below HTML file and when I manually move cursor on the dummy link, it shows me yellow color, as I have added that style on css hover. Same I would like to simulate with java script. Here below is the image for reference.

image

Please suggest me how to achieve it?

<!DOCTYPE html>
<html>
<head>
<style>
a:hover {
   background-color: yellow;
}
</style>
</head>
<body>

<a href="https://####">dummylink</a>

<p><b>Note:</b> The :hover selector style links on mouse-over.</p>

</body>
</html>
3
  • 1
    Is there a reason why using css isn't an option? Commented May 4, 2018 at 5:36
  • try this link may be it help, stackoverflow.com/questions/11371550/… Commented May 4, 2018 at 5:39
  • try this i hope it will help you <a href="https://####" onmouseover="style.background='yellow'" onmouseout="style.background='white'" >dummylink</a> Commented May 4, 2018 at 6:24

1 Answer 1

3

Use the mouseover and mouseout events:

const a = document.querySelector('a');
a.onmouseover = () => a.style.backgroundColor = 'yellow';
a.onmouseout = () => a.style.backgroundColor = null;
<a href="https://####">dummylink</a>

Sign up to request clarification or add additional context in comments.

2 Comments

How do I know the color of element and which style was added for hover.I just want to do the same action which I did manually with javascript i.e mouse hover.Actually it is for Regression testing in which we automate the actions to test the functionalities.
If you want to assign the color string to a variable and use it elsewhere, feel free to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.