Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  104] [ 5]  / answers: 1 / hits: 24244  / 10 Years ago, tue, august 26, 2014, 12:00:00

I have some Javascript which displays an element on hover. I want to style this element and therefore need to trigger the hover state in the browser using Chrome Dev Tools.



This is easy to do with CSS where you can set the state of an element within Dev Tools. What is the best way to do this with Javascript?



Code Example:



$('#menu').hover(
function() {
console.log('test');
$('#dropdown').show();
},

function() {
$('#dropdown').hide();
}
);

More From » jquery

 Answers
6

Take the below snippet of a menu which shows a dropdown on hover:





$('#menu').hover(
function() {
$('#dropdown').show();
}, function() {
$('#dropdown').hide();
}
);

#menu {
width: 100px;
background-color: #03f;
color: #fff;
padding: 2px 5px;
}
#dropdown {
width: 100px;
background-color: #03f;
color: #fff;
padding: 2px 5px;
display: none;
}

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<div id=menu>menu</div>
<div id=dropdown>
<ul>
<li>menu item 1</li>
<li>menu item 2</li>
<li>menu item 3</li>
</ul>
</div>





Copy this snippet into a local document, as Chrome Dev Tools will not allow you to use Javascript to select any element in this iframe. Then, in your Dev Tools Console, run:



$('#menu').trigger('mouseover');


This will show the dropdown menu, which has a really ugly, unstyled list. Now, instead of using your mouse to right-click the element and selecting Inspect Element, which I would imagine is how your're used to doing things, run in your Console:



$('#dropdown');


Or whatever selector for whichever element you want to style/manipulate. The Console will show the result of your statement, which is the relevant jQuery object. Now right click on that object in your Console and select Reveal in Elements Panel. Now you can use the Styles tab as you're used to, and your mouse has never triggered the mouseout event, ending the hover.


[#69652] Saturday, August 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carolinabritneyp

Total Points: 75
Total Questions: 102
Total Answers: 105

Location: Armenia
Member since Fri, Apr 16, 2021
3 Years ago
;