8

Is it possible to access the $0 DOM element pointer from bookmarklet or snippet in console.?

More info about $0

https://developers.google.com/chrome-developer-tools/docs/commandline-api#0_-_4

Dev Tools remembers the last five DOM elements (or JavaScript heap objects) that you've selected in the tab (or Profiles panel). It makes those objects available as $0, $1, $2, $3, and $4. $0 returns the most recently selected element or JavaScript object, $1 returns the second most recently selected one, and so on.

venkat
  • 2,310
  • 1
  • 15
  • 15
  • Your question contains "in console", to that the obvious answer is yes, since if you copy any javascript code that has $0 and run it in the Console tab of DevTools it will work, but not on the bookmarks bar. – TWiStErRob Apr 12 '15 at 22:08
  • It's supported now in Snippets. See [my answer](https://stackoverflow.com/a/48124238/1669860). – Kayce Basques Jan 06 '18 at 04:25

3 Answers3

0

I have been looking for a way to do this too. I haven't found one, but I found a good compromise to be a bookmarklet that copies a snippet of code to my clipboard. I use $0 with angular a lot to get the scope of an element, like this: angular.element($0).scope()

So I have a bookmarklet that copies that line to my clipboard, then I can very easily paste that into the console. Depending on what you want to use $0 for, maybe that will do the trick for you.

Here is a link to how to copy text to clipboard with javascript: How do I copy to the clipboard in JavaScript?

Community
  • 1
  • 1
user3692823
  • 341
  • 1
  • 11
0

In the bookmark I simply used angular.element(this.document) instead of angular.element($0) and it worked for me.

Vadim Kirilchuk
  • 3,532
  • 4
  • 32
  • 49
0

This seems to be working as of Chrome 65.

I've got a Snippet that runs:

console.log('$0 points to', $0);

In the GIF (Right-click > Open Image In New Tab if it's too small) you can see that the reference gets updated every time that I select a new element and then re-run the snippet. FYI I'm running the Snippet from the Command Menu by typing ! followed by the name of the script.

enter image description here

Kayce Basques
  • 23,849
  • 11
  • 86
  • 120