Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript dollar sign variable not working

I have following code in my Wordpress:

(function ($) {
  var $header = $("div.header");

  $(window).bind("scroll resize", function () {
    if ($(window).scrollTop() > 30) {
      $("div.header").stop().animate({
        'opacity': 0.24
      }, {
        duration: 1000
      });
    } else {
      $header.stop().animate({
        'opacity': 1
      }, {
        duration: 1000
      });
    }
  });
})(jQuery);

If statement kicks in when supposed but else never...

BUT

If I enclose it with:

jQuery(document).ready(function($) {        
  // code here
});

It's all fine. Why?

Thank you

like image 468
daniel.tosaba Avatar asked Sep 05 '12 04:09

daniel.tosaba


People also ask

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

Can JavaScript variables start with dollar sign?

Naming variables Variable names are pretty flexible as long as you follow a few rules: Start them with a letter, underscore _, or dollar sign $. After the first letter, you can use numbers, as well as letters, underscores, or dollar signs. Don't use any of JavaScript's reserved keywords.

Can a variable have a dollar sign?

The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.

What is a dollar sign variable in JavaScript?

JavaScript Dollar Sign Variable When using jQuery, developers usually prefer to use a variable name with a dollar sign. It’s a personal practice to identify that it contains a jQuery object. Here is a code example of jQuery with a dollar sign variable.

How do you use dollar sign in a string?

Dollar Sign in Template Literals In JavaScript we use $ sign and then { } to embed expression in string. It allows multi-line string and string interpolation features. Example 1: To Embed and evaluate an arithmetic express inside a string.

What is the dollar function in jQuery?

The dollar function is a function that takes in some sort of identifier for an HTML element or elements and returns a jQuery object. It’s the main function used by jQuery that’s the basis for the whole framework.

Is it possible to create your own dollar sign function?

That also means that creating your own dollar sign function that does something different is probably not a good idea. In case you were wondering, the underscore has no special properties either. But let’s face it, $ (‘myVal’) looks more like programming than _ (‘myVal’).


1 Answers

May be you're trying to use jQuery when dom in not build. Try to use $(document).ready function:

(function ($) {
  $(document).ready(function () {
    $header = $("div.header");
    $header.remove();
  });
})(jQuery);

About what you have mantioned in the question:

jQuery(document).ready(function ($) {
  // code
});

It works because it do the same thing: it binds event handler on ready event and pass jQuery object as a parameter to the function as $.

Now what you did before:

(function ($) {
  $header = $("div.header");
  $header.remove();
})(jQuery);

Here you just declare anonymous function with named $ parameter:

function ($) {
}

And call it with jQuery object as a parameter, which will be available in the function as $:

(function ($) {
})(jQuery);
like image 160
Danil Speransky Avatar answered Oct 21 '22 16:10

Danil Speransky