Michael Borgwardt
Michael Borgwardt

Reputation: 346317

function as parameter to $ in 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(){

Upvotes: 4

Views: 113

Answers (2)

Thariama
Thariama

Reputation: 50832

jQuery API tells us:

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

  • callback - The function to execute when the DOM is ready.

Upvotes: 3

Nick Craver
Nick Craver

Reputation: 630429

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(){

Upvotes: 9

Related Questions