17

It appears that jQuery selectors are not functioning in the Chrome Console. Any type of selector returns "null". The scripts do properly run the javascript, however.

Has anyone else noticed this change or know of a fix.

Thanks.

3
  • 2
    Can you post examples of code that's not working? Jan 25, 2011 at 16:28
  • 1
    in short, use jQuery in console instead of $
    – xster
    Apr 21, 2011 at 14:51
  • at 2012-01-23 accessing jquery via $ is now working in Chrome 16.0
    – mozboz
    Jan 23, 2012 at 17:28

2 Answers 2

17

I uncovered the cause of this in my own question.

The console injects its own function (just a shorthand) for document.getElementById(), aliased to $, which shadows jQuery's $. Easy way to check this: when you're at a breakpoint, and jQuery seems to be broken, compare the following in the console:

  • jQuery
  • $
  • window.$

The first and last will be jQuery proper, the local $ is something like:

function () {
    return document.getElementById.apply(document, arguments)
}

This is because code run from the console is wrapped in a with statement:

with (window ? window.console._commandLineApi : {}) {
with (window) {
    // the actual code you typed in here
}   
}

and window._commandLineApi.$ is the function that shadows jQuery.

stupid chrome


Found the bug in Chromium for this: http://code.google.com/p/chromium/issues/detail?id=70969

2
  • 3
    or, just type in jQuery===$. It will return true if jQuery has control of the $ sign.
    – Yahel
    Feb 12, 2011 at 4:05
  • 1
    (and, jQuery will return a different looking function, depending on which version of jQuery you're running, so this is easier)
    – Yahel
    Feb 12, 2011 at 4:05
0

Just run the following command on the console to make it work:

$ = jQuery

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.