Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function as parameter to $ in jQuery

Tags:

jquery

...what does it mean? I have almost no experience with jQuery, and need to work with some existing code.

All the tutorials talk about is using $() with pseudo-CSS selectors, but what would be the meaning of something like this:

$(function makeFooWriteTooltip() {
    if($("div[name='txttooltip']").length>0){
        $("div[name='txttooltip']").each(
         function(){
like image 914
Michael Borgwardt Avatar asked Oct 01 '10 13:10

Michael Borgwardt


People also ask

What is FN in jQuery?

fn is an alias for jQuery. prototype which allows you to extend jQuery with your own functions. For Example: $.fn.

How write a function in jQuery call it?

Answer: Use the syntax $. fn. myFunction=function(){} The syntax for defining a function in jQuery is little bit different from the JavaScript.

How do you pass a function as a parameter in TypeScript?

Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.

What is function parameter in JavaScript?

Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function.


2 Answers

It's a shortcut for:

$(document).ready(function makeFooWriteTooltip() {

Though, the function need not have a name here. Passing a calback to $() runs the function on the document.ready event, just a bit shorter, these are equivalent:

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

Also, given your exact example, there's no need to check the .length, if it's there it runs, if not the .each() doesn't do anything (no error), so this would suffice:

$(function () {
  $("div[name='txttooltip']").each(function(){
like image 186
Nick Craver Avatar answered Oct 25 '22 10:10

Nick Craver


jQuery API tells us:

jQuery( callback ) ( which equals to $(callback) )

  • callback - The function to execute when the DOM is ready.
like image 30
Thariama Avatar answered Oct 25 '22 10:10

Thariama