Reputation: 2338
I have a js file which has several fn() and it was working fine but when at tached a js plugin file, i get error in firebug.
$(document).ready(function() {
var innerHTML = $("#id_1").html();
$("#Id_2").html(innerHTML)
});
above fn() was working but now i am getting error in firebug $("#id_1") is null
.
if i use jQuery
instead of $
then it's workin fine. please see below the corrected function.
$(document).ready(function() {
var innerHTML = jQuery("#id_1").html();
jQuery("#Id_2").html(innerHTML)
});
now my problem is, i have several js files and i have used $
and now i might have to change $
to jQuery
. please give me any idea to avoid this.
Upvotes: 3
Views: 150
Reputation: 22710
It looks like you are missing #
$(document).ready(function() {
var innerHTML = $("#id_1").html();
$("#Id_2").html(innerHTML); // ; is optional here but good to have it
});
Assuming you have elements with id
values id_1
and Id_2
.
Just check case for Id_2
.
Working fiddle
Upvotes: 1
Reputation: 30666
The extra library you are using (wysiwyg.js) is also declaring a function $()
:
/**
* Get an element by it's identifier
*
* @param id Element identifier
*/
function $(id) {
return document.getElementById(id);
}
You should use jQuery.noConflict()
Upvotes: 2
Reputation: 18568
you can directly do it like
$("#id_2").html($("#id_1").html());
check fiddle : http://jsfiddle.net/nUZer/2/
Upvotes: 0
Reputation: 74202
It is evident that the problem is caused by another JavaScript library defining the $
function. You can work around it by:
var $copy = $; // Copy the original value of $ to a temporary variable
$ = jQuery;
// Do everything that needs to be done with the jQuery library */
$ = $copy; // Copy the old value of $
Or:
( function($) {
// $ has been redefined for the scope of the current function
// Do everything that needs to be done with the jQuery library */
} )(jQuery);
Upvotes: 2
Reputation: 3214
Actually, jQuery
and $
is the same thing. If you look into jQuery source, you will find something like:
window.jQuery = window.$ = function(){ ... }
In your case it seams, that the window.$
variable was replaced with some other. It is possible, that some of your files called $.noConflict()
and the $
variable got replaced.
Upvotes: 1
Reputation: 5279
$
is just an alias for 'jQuery' .
Please make sure the library you are adding is not making any conflict with '$' . It generally happens when you try adding another framework with '$' as alias ex prototype.
use jQuery no Conflict http://api.jquery.com/jQuery.noConflict
Upvotes: 2