Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 284129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:25:55+00:00 2026-05-12T05:25:55+00:00

I am modifying some code that has a lot of jQuery but I am

  • 0

I am modifying some code that has a lot of jQuery but I am not sure what some of the jQuery statements are doing.

At the top of the jQuery code there is

jQuery.noConflict

*1. I understand that. But then there is a some code that has:

<script type="text/javascript">
(function($) {

$(document).ready(function() {

    jQuery.fn.fixEmail = function() {
    {
   return $(this).each(function() {
       var $s = $(this);                  
           ...code...
        }
}
</script>

I get that jQuery is used because of the noConflict. What’s the parameter $?

*2. In another function, they use

<script type="text/javascript">
jQuery(function(){
    var $ = jQuery;
    var cc = {
        mode : 'teaser',
        featureVisible : true,
        $loader : '<p class="loadingAnimation"><img height="32" src="' +
                config.xoImgUrl +
                '/images/ajax-loader.gif" width="32" /></p>',
                ....more code...
            }
}
</script>

So they are setting $ to the jQuery from noConflict. But why? Could they have just used jQuery?

*3. There is a plugin that I want to use that is initialized by:

   var $j = jQuery.noConflict();
    var $ = {};
    $j(document).ready(function(){
        $j.history.init(pageload);
        $j("a[@rel='history']").click(function(){
            ...more code...
        });
    });

I understand what the noConflict does but what does var $ = {} do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:25:55+00:00Added an answer on May 12, 2026 at 5:25 am

    Example 1:

    I think you missed out on some code:

    (function($) {
    
    $(document).ready(function() {
    
        jQuery.fn.fixEmail = function() {
        {
       return $(this).each(function() {
           var $s = $(this);                  
               ...code...
            }
    }
    )(jQuery); //This line was missing in your code. 
    

    Let’s rewrite this code a little bit to understand what’s going on.

    function complicatedFunction($) {
              // the document.ready call goes here.
    }
    

    Next, how would you call this function?

    complicatedFunction(someObject);
    

    So inside the complicatedFunction $ refers to someObject. Agree?

    If you write

    complicatedFunction(jQuery);
    

    Then inside the function, $ refers to the jQuery object. So everything inside, complicatedFunction can use ‘$’ as like how a normal jQuery user would do.

    Coming back to the original code, if we decide to not name this function i.e. make it anonymous, you can visualize the code like,

    (function($) { })(jQuery);
    

    You are creating an anonymous function, taking one argument named $.
    You immediately call this anonymous function passing it the jQuery object.
    This way, you don’t modify the global $ object but all the code inside your anonymous function works like $ was always available. Cool, isn’t it? 🙂

    Example 2:

    jQuery(function(){
            var $ = jQuery;
            var cc = {
                    mode : 'teaser',
                    featureVisible : true,
                    $loader : '<p class="loadingAnimation"><img height="32" src="' +
                                    config.xoImgUrl +
                                    '/images/ajax-loader.gif" width="32" /></p>',
                    ....more code...
                }
    });
    

    Similar to example 1, I have not seen this style of coding. (I am not even sure if this would work)

    Anyway, inside the anonymous function, which is passed to the jQuery, you localize the usage of $ without impacting other code.
    And yes, they could have simply used jQuery object everywhere but that will be very verbose, isn’t it?

    Example 3:

    var $ = {};
    

    Above line defines an empty object and assigns it to the variable $.

    It is same as doing,

    var $ = new Object();
    

    This is similar to how you can define an array using two different syntaxes.

    var items = [];  //same as var items = new Array();
    

    A little history

    Remember, there is nothing inherently special about ‘$’. It is a variable name just like any other. In earlier days, people used to write code using document.getElementById. Because JavaScript is case-sensitive, it was normal to make mistake while writing document.getElementById. Should I capital ‘b’ of ‘by’? Should I capital ‘i’ of Id? You get the drift. Because functions are first class citizens in JavaScript, you can always do this

    var $ = document.getElementById; //freedom from document.getElementById!

    When Prototype library arrived, they named their function, which gets the DOM elements, as ‘$’. Almost all the JavaScript libraries copied this idea. Prototype also introduced $$ function to select elements using CSS selector.

    jQuery also adapted $ function but expanded to make it accept all kind of ‘selectors’ to get the elements you want. Now, if you are already using Prototype in your project and wanted to include jQuery, you will be in problem as ‘$’ could either refer to Prototype’s implementation OR jQuery’s implementation. That’s why jQuery has the option of noConflict so that you can include jQuery in your project which uses Prototype and slowly migrate your code. I think this was a brilliant move on John’s part! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am modifying some code and came across a declaration that I am having
I am modifying some Flex code written by someone else. There is an mx:text
I'm doing some tests with nhibernate and I'm modifying batch_size to get bulk inserts.
I'm modifying existing security code. The specifications are pretty clear, there is example code,
I've been doing some web development work in PHP recently which has led me
I'm currently modifying a class that has 9 different constructors. Now overall I believe
I have a class CContainer that has some members CMemberX , CMemberY , which
I would like to add some code to my Application.cfc onRequestEnd function that, if
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
I have some code that is structured as follows from my.modules import MyClass Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.