0

I'm learning jQuery by writing some code in Chrome's console, e.g. adding an id to a DOM element and then executing jQuery functions.

On some websites (for example Google) I get Uncaught TypeError and on others, I don't. Here is an example:

DOM modification:

<input id="test" aria-label="Szukaj w Google" name="btnK" type="submit" jsaction="sf.chk">

Console:

$("#test").hide()

Error:

VM598:1 Uncaught TypeError: $(...).hide is not a function
at <anonymous>:1:12

The same flow on other websites (for example StackOverflow) works. What is the reason?

user7637745
  • 965
  • 2
  • 14
  • 27
SigGP
  • 716
  • 1
  • 11
  • 24

1 Answers1

1

jQuery is not loaded on many websites on the internet. That's why you get your error message Uncaught TypeError: $(...).hide is not a function, because actually the $ is not jQuery, rather another function, object, or it's just simply undefined.

Although you can find one of many versions of jQuery loaded on numerous websites, several companies/projects/websites don't use jQuery, or they use a custom build of it, or something else under the $ sign, or just not at all.


On a website, by default:

If that particular website doesn't override those variables with another implementation (e.g. custom function, library, jQuery, etc.). More on this

Credit to @AuxTaco


If you need to have jQuery on a website (e.g. for development purposes), where it's not present, you can inject it into the website.

user7637745
  • 965
  • 2
  • 14
  • 27
  • But when I execute for example $("#test").value = "newValue" it works on google and I can change string on search button, so I assume that jQuery is loaded on google – SigGP Jun 30 '18 at 01:07
  • 1
    Probably they have a custom library under the `$`, or it's a custom-built jQuery. If you use Chrome, you can check it out under the Network and Sources tabs, what code do they really use under the `$` sign. – user7637745 Jun 30 '18 at 01:11
  • 2
    @SigGP It's very common to use `$` as a short name for a function that looks up elements. But it's not always jQuery. – Barmar Jun 30 '18 at 01:12
  • 3
    When `$` is not otherwise defined, [it's an alias for `document.querySelector`](https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#queryselector) in the console. – AuxTaco Jun 30 '18 at 01:13