user3494047
user3494047

Reputation: 1693

what does it mean to wrap an in place function with a jquery selector?

I am confused about what is happening here:

$("a[class='delete']").click(function(e) {
   $( function() {
     $( "#dialog" ).dialog({});
   } );
});

I see that at first a link element is selected and then when a click event on that link happens a function is executed. That function says to select (I guess because of the $ sign) whatever the output of another function is. Then that next function selects an element with a dialog class and runs the dialog function on it.

Practically what happens is that the html element that has class dialog comes up on the screen as a dialog box. My question is why is the function which selects the dialog box element inside of a selector?

I know when I delete the selector wrapping the function() there is a syntax error (which I don't quite understand) but why not use this code:

$("a[class='delete']").click(function(e) {
      $( "#dialog" ).dialog({
       });

 });

Upvotes: 2

Views: 100

Answers (1)

Pointy
Pointy

Reputation: 413737

Passing a function to $() is the same thing as

$(document).ready(function() { /* something */ });

In your case, it looks like code written by a confused person. There's no reason to set up code as a "ready" handler in response to a "click" event, at least in general. This should be equivalent:

$("a[class='delete']").click(function(e) {
   $( "#dialog" ).dialog({});
});

Upvotes: 4

Related Questions