Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why $() doesn't work but jQuery() works?

Tags:

jquery

I have an issue where $("#register") returns null, but jQuery("#register") returns object correctly.

This may be worth thousand words:

Chrome inspector expressing the problem

As you can see in Watch expression everything works as expected (jQuery === $ returns true) But in console it doesn't. And in code it also doesn't, because the $("#register").validate string doesn't work.

Maybe it has to do something with $script.js thing?

like image 792
Janis Veinbergs Avatar asked Apr 01 '11 07:04

Janis Veinbergs


People also ask

Can I use both JavaScript and jQuery together?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

What is $() in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

When we should use $( this in jQuery?

The value of this inside a click event is a DOM element (the one that was clicked). Using $(this) converts it to a jQuery object. DOM elements do not have a hide() methods, but jQuery adds that and many other methods you can then call.


2 Answers

Maybe some other javascript framework that you are using hijacks the $() function. You may take a look at jQuery.noConflict() for tips about using jQuery with other frameworks.

like image 143
Darin Dimitrov Avatar answered Sep 18 '22 17:09

Darin Dimitrov


I'm sorry to return in 5 minutes and say that the code works. Uh.

It's just that $ is something the web inspector overrides.

The $script.js doesn't cause conflicts.

> jQuery.noConflict()
function (a,b){return new d.fn.init(a,b,g)}
> $
function ()
    {
        return document.getElementById.apply(document, arguments)
    }

It's the chrome inspector problem :)

like image 38
Janis Veinbergs Avatar answered Sep 17 '22 17:09

Janis Veinbergs