What is the difference between $ and jQuery
When I try to use $("#div_id")
in $(document).ready
it returns NULL, but when I use jQuery("#div_id")
it returns the actual object!
Why is that happening?
UPDATE: I tried noConflict method without gaining any hints.
jQuery.noConflict()
function (a,b){return new c.fn.init(a,b)}
$.noConflict();
TypeError: Object function ()
{
return document.getElementById.apply(document, arguments)
} has no method 'noConflict'
UPDATE 2:
$(document).ready(function() {
debugger;
});
<input type="text" id="test" name="test" value="123" />
When I run the following code in the console i got those results:
$("#test").val()
TypeError: Cannot call method 'val' of null
jQuery("#test").val()
"123"
Thanks
Solution 1:
See jQuery.noConflict(). Could other javascript libraries on your page be using the $
variable?
$
is just a variable that is used to alias jQuery
and being a variable, anything could be assigned to it.
Solution 2:
$
and jQuery
both are same except $
is just an alias of jQuery
which you can change or remove with jQuery.noConflict
mode.
Solution 3:
$ is also used in Prototype.js, which is a javascript framework like jQuery. It could be the case that your project also includes references to this framework.
For more info on how the prototype dollar works, go here.
You can make the two work together with the noConflict statement, as stated in the other answers. We're doing this in one of our projects, which uses the $(element) for legacy prototype code, and jQuery(element) for new jQuery code.
Solution 4:
Looks like something has taken the $, to reassign jQuery to $, try going:
var $ = jQuery;