Search code examples
javascriptjquerybrowser-console

Using jQuery functions on some websites cause Uncaught TypeError


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?


Solution

  • 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.