I often want to view the styles of an element that appears only when dragging or when the mouse is clicked (mousedown
event). How can I view the element's style using Google Chrome's developer tools?
-
Duplicate of stackoverflow.com/questions/7540379/inspect-helper-clone– gatMar 23, 2014 at 22:31
8 Answers
Open the developer tools.
Go to "Sources":
Expand "Event Listener Breakpoints" on the right:
Add a listener for keydown events on the keyboard section:
Now start dragging the thing you want, and when it's time press any key on your keyboard and you'll be able to inspect the dragable element.
-
1I always set the listener on Mouse/mouseup so it will automatically break when you stop dragging Feb 22, 2021 at 22:33
You can simply press F8
while dragging (and developer tools is open)
-
1This does not work for my mac. Does anyone know the macOS shortcut? Feb 22, 2021 at 22:29
-
@FelixGeenen F8 works on macOS. Make sure you are pressing
fn
key. Jun 28, 2021 at 9:24 -
1@NyproTheGeek I tried it again but couldn't manage to get it working on my mb pro 16" with touchbar and Big Sur 11.4 and chrome 91 Jun 29, 2021 at 11:40
In case anyone encountered this question in the future, I have another solution for this. This solution is kinda same with the most upvoted answer, but it doesn't require any keydown, just simply drag:
- Open chrome devtools
- Click on the Sources tab
- Go to Event Listeners Breakpoints down there
- Om the event list, click Drag / drop, then tick dragover
After that, whenever you start to drag an element, the browser window will pause for debugging, then you can inspect the element's CSS styles freely.
Note: I tested this on Chrome version 80, but I think it works in older version though.
Edited:
Just now I figured out dragover breakpoints doesn't work in certain condition, e.g., if you want to inspect styles after the dragged item reached another element. For that situation, you may try different listeners as specify in Drag / drop, such as drop.
dragMethod() {
setTimeout( () => {
debugger;
}, 500)
}
This will suspend the drag action so you can proceed to inspect as normal.
One way of doing it is to open the elements panel then right click while dragging. This opens the contextual menu and "pauses" the mouse move/hover effect. Then after right clicking, go back to the elements panel and search for the element using the find feature.
This can also be used to inspect hover effects (it's just faster than other methods)
This can be tested here for example https://jqueryui.com/draggable/#visual-feedback
-
If I go to jqueryui.com/draggable/#events , it indicates that when I right-click, the "stop" event is invoked, just as if I released the left button. Shouldn't the number of stop events remain zero for what the OP wants? Jan 20, 2020 at 23:30
From the DevTools Go to the lowest element that will wrap your draggable item
Right click this element and chose "Store as global variable" it'll be referred to from the console as
temp1
Write in the console this command -
let myInterval = setInterval(() => console.log(temp1.cloneNode(true)), 1000)
At this stage you can see the element details in the console whem you drag it.
When you don't need to inspect it any more run from the console - clearInterval(myInterval)
.
Instead of section 2 you can run the follow command and select your draggable element with the appropriate query selector - let myInterval = setInterval(() => console.log(document.querySelector(/* your query goes here */)?.cloneNode(true)), 1000)
Put a breakpoint in the code - inside of the mousedown
event callback.
This will freeze the app when you begin dragging, and then you can tab over the the Element
section of the inspector to use it like you normally would, only now it's frozen at the beginning of the drag.
EDIT: You should put the breakpoint on a line below where the new elements you want to inspect are created, so the elements are on the DOM by the time you freeze.
// Raw event
someElement.addEventListener('mousedown', function(ev) {
// Put a breakpoint on any of the lines in here
}, false);
// jQuery for prudence
$(someSelector).on('mousedown', function(ev) {
// Put a breakpoint on any of the lines here
});
In addition to @Davids answer, it might be worth mentioning, that you need to add a eventlistener somewhere in your code as well or simply put it in the console before
Example:
document.onclick=function(){};