Function as parameter in $ in jQuery

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

All talk about using $ () with pseudo-CSS selectors, but what would something like this mean:

$(function makeFooWriteTooltip() {
    if($("div[name='txttooltip']").length>0){
        $("div[name='txttooltip']").each(
         function(){
+5
source share
2 answers

This is a shortcut for:

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

Although the function here should not have a name. Passing calback into the $()function starts in the event document.ready, a little shorter, this is equivalent:

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

Also, given your exact example, there is no need to check .lengthif it works there, if it doesn’t .each()do anything (without errors), so this would be enough:

$(function () {
  $("div[name='txttooltip']").each(function(){
+10
source

The jQuery API tells us :

jQuery ( ) ( $( ))

  • callback - , DOM.
+3

All Articles