Hacker News new | ask | show | jobs
by amatecha 1860 days ago
Here's a really obscure one which I've never heard anyone mention, except the first time I heard about it:

In the JS console:

  $0
is a reference to the currently-highlighted element in the DOM Inspector (at least this is the case in Firefox, Chrome and Edge, haven't tried others).

Very handy for quickly evaluating or operating on an element you're looking at :)

4 comments

You'll see that in the Elements panel, it actually puts a grey " == $0" after the selected element to point you towards this feature.

Also, $1 is the previously selected element, $2 is the one before that, etc.

Author here. There are even more. $0 is only the current one, but you also have $1 to $4 as a history of the last ones/ I like that $_ is a reference to the last result of what you did in the console. I documented most here: https://docs.microsoft.com/en-us/microsoft-edge/devtools-gui...
Nice, that's great to know! Thanks for capturing them on that page. BTW I've followed you on Twitter for years. Always lots of neat tips and interesting topics -- thanks for that! :)
In a similar vein you can right click on any logged value that's pretty printed in color and save it to a global variable to play around with. (console.log("foo", foo) works with this, console.log("foo"+foo) doesn't)
If you’re using React the same applies in the dev tools component tree - $r is the currently highlighted component.