user1010399
user1010399

Reputation: 2338

what is basic difference between $ Vs JQuery

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

Answers (6)

Ajinkya
Ajinkya

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

Didier Ghys
Didier Ghys

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

dku.rajkumar
dku.rajkumar

Reputation: 18568

  1. you are missing '#' in second id.
  2. you are missing ';' at the end of id_2 line
  3. you have given uppercase 'I' in 'Id_2'. make it sure that it is correct.

you can directly do it like

$("#id_2").html($("#id_1").html());

check fiddle : http://jsfiddle.net/nUZer/2/

Upvotes: 0

Alan Haggai Alavi
Alan Haggai Alavi

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

Alexander Yezutov
Alexander Yezutov

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

Gaurav Shah
Gaurav Shah

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

Related Questions