What's the .apply jQuery function?

Asked
Active3 hr before
Viewed126 times

8 Answers

functionjqueryapply
90%

apply calls a function with a set of arguments. It's not part of jQuery, it's part of core Javascript. However, there is mention of it in the jQuery docs:, Is there a way to make an air conditioner without venting heat outdoors? , How to not publish in peer-reviewed journals and still be taken seriously , Podcast 378: The paranoid style in application development

Syntax:

somefunction.apply(thisObj, [argsArray])
88%

In JavaScript strict mode, if the first argument of the apply() method is not an object, it becomes the owner (object) of the invoked function. In "non-strict" mode, it becomes the global object.,Since JavaScript arrays do not have a max() method, you can apply the Math.max() method instead.,If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:,The first argument (null) does not matter. It is not used in this example.

Method Reuse

With the apply() method, you can write a method that can be used on different objects.

apply()
load more v
72%

context Type: Element or jQuery or Selector A DOM Element, Document, jQuery or selector to use as context ,This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked item in the this keyword as a bare DOM element, the element must be passed to the $() function before applying jQuery methods to it.,jQuery( selector [, context ] ), element Type: Element A DOM element to wrap in a jQuery object.

$("div.foo");
load more v
65%

The first and most obvious way to declare a function in JavaScript is to use a function declaration. A function named multiply(), which takes two parameters x and y, multiplies them, and returns the value can be implemented with the following syntax:,Functions defined in this way (a function declaration) are hoisted to the top of the current scope. The console.log() could be placed before the function and it would still work.,In this article, we examine several ways to define a block of JavaScript functionality.,Choosing which way to declare a JavaScript function can be confusing for beginners and there are several different syntax options. Each has advantages, disadvantages, and appropriate uses that can catch you out.

The first and most obvious way to declare a function in JavaScript is to use a function declaration. A function named multiply(), which takes two parameters x and y, multiplies them, and returns the value can be implemented with the following syntax:

function multiply(x, y) {
   return x * y;
}

console.log(multiply(2, 2));
// output: 4
load more v
75%

The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).,What if that is not what you want? concat does have the desired behavior in this case, but it does not append to the existing array—it instead creates and returns a new array.,The result of calling the function with the specified this value and arguments.,Clever usage of apply allows you to use built-in functions for some tasks that would probably have otherwise been written by looping over the array values.

apply(thisArg)
apply(thisArg, argsArray)
load more v
40%

Below examples illustrate the function definition in jQuery:,How to add options to a select element using jQuery?,How to check the user is using Internet Explorer in JavaScript?,How to get the ID of the clicked button using JavaScript / jQuery ?

Syntax:

$.fn.myFunction = function() {}
load more v
22%

$ is a short form of jQuery function. $() = jQuery() = window.$() = window.jQuery() , $(document).ready(); , Now, you can start interacting with DOM using jQuery safely in the callback function. , When you open your web page in a browser and load jQuery library successfully, it adds a global function named jQuery(). The global scope means scope of the window, so the global jQuery() function can be called like window.jQuery() or jQuery() anywhere in your web page. $ is an alias of jQuery function, so you can also use $() as a short form of jQuery().

jQuery('div')

//Or

window.jQuery('div')

//Or

$('div')

//Or

window.$('div')
load more v
60%

The DOM object gives you direct access to the “src” property of the image because you have a node of type “image” that has access that property. In the case of jQuery, everything is a jQuery object, so you have to go through the attr function to set the image source.,If you are adhering to the “scripts at the bottom” best practice, then you have no need for jQuery’s document ready function as the HTML is already loaded by the time the script is run.,That function finder looks pretty interesting. I’ll have to try that out sometime.,Since jQuery is changing the scope for you in these loops, it’s a good idea to store the reference to this somewhere so that you know it’s not going to change on you.

If you are adhering to the “scripts at the bottom” best practice, then you have no need for jQuery’s document ready function as the HTML is already loaded by the time the script is run.

<p id="zack">This element is on the page <strong>BEFORE</strong> all the scripts. No document ready needed.</p>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

  <script type="text/javascript" charset="utf-8">

    // if you include your scripts at the very bottom, you don't need document ready
    (function($) {

      $("#zack").css("color", "green");
      $("#slator").css("color", "red");

    }(jQuery));

  </script>

<p id="slater">This element comes after the scripts and won't be available.</p>
load more v

Other "function-jquery" queries related to "What's the .apply jQuery function?"