1

When we type $ in chrome console it returns a function. I am sure it's not jQuery's $.
If I want to use jQuery in console, What is the best way to trigger jQuery in chrome console as $.

Jagajit Prusty
  • 2,070
  • 2
  • 21
  • 39

1 Answers1

5

$ is an alias for document.querySelector. In the same vein there is $$ which is an alias for document.querySelectorAll.

It is defined in the Command line (console) api.

Command Line API Reference

The Command Line API contains a collection of convenience functions for performing common tasks: selecting and inspecting DOM elements, displaying data in readable format, stopping and starting the profiler, and monitoring DOM events.

If you have $ defined on the page as a global (perhaps by using jQuery), you'll get that global, not the command-line built-in.

There are other handy functions defined there.


To run jQuery, without having it in the page source code, you may find Chrome extensions to be handy, or simply copy-paste the jQuery source code in the console.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • I don't have jQuery as global then how can I run jQuery as global? – Jagajit Prusty Aug 18 '17 at 13:22
  • @JagajitPrusty: If you include jQuery on the page, by default it's global (it defines global `$` and `jQuery` variables). You have to do something intentionally to make it give those up (http://api.jquery.com/jQuery.noConflict/). – T.J. Crowder Aug 18 '17 at 13:23
  • Yeah I know that. I was asking about easy way to do so through some plugin may be such as fireQuery for Firefox. – Jagajit Prusty Aug 18 '17 at 13:26
  • @JagajitPrusty If you don't have jQuery as global, `$` will be the same with `document.querySelector`—this is specific to Google Chrome's console, as mentioned. If `window.$` is a function (e.g. `window.$ = function () {}`), that will obviously have higher priority—`$` will not point to `querySelector` but to your function (which can be `jQuery` or whatever you defined). – Ionică Bizău Aug 18 '17 at 13:26
  • @JagajitPrusty To expose jQuery as a global, you just have to do `window.$ = window.jQuery = require("jquery")` (replace `require("jquery")` with your jQuery function which is not a global). The idea is: by appending it to the `window` object, it becomes a global. – Ionică Bizău Aug 18 '17 at 13:27
  • I don't have jQuery at all. Still I want to run some jQuery code for testing purpose. Then is there any plugin which I can enable so that it will make jQuery global for that tab? – Jagajit Prusty Aug 18 '17 at 13:30
  • @JagajitPrusty [Yes, there are extensions](https://chrome.google.com/webstore/detail/jquery-injector/ekkjohcjbjcjjifokpingdbdlfekjcgi). Or you can simply copy-paste the jQuery code in the console and run it. – Ionică Bizău Aug 18 '17 at 13:42