0

What is the meaning of this notation:

$("#linka").click(function() {

   // some codes

}).fancybox({

   // some options

});

What is difference between that and these codes:

$("#linka").click(function() {

   // some codes

});

$("#linka").fancybox({

   // some options

});

Thank you.

1
  • 2
    You should look into 'method chaining'. Commented Mar 5, 2013 at 11:16

3 Answers 3

1

The end result will be the same, but the second code snippet has an unnecessary call to the jQuery function to re-select the element. Most jQuery methods return a jQuery object, with either the same or an updated set of elements, allowing other jQuery methods to be called afterwards.

It's called method chaining, and allows you to select element(s) once and interact with them using multiple methods.

Sign up to request clarification or add additional context in comments.

Comments

1

With jQuery you can chain methods instead of having to re-define the selector each time. Both of the code samples above do exactly the same thing. The first method merely omits the unnecessary call to the jQuery function.

Comments

1

you'll get same result for both methods. chain method concept is applied here with method one.here you don't have to declare the selector again.

in method two,you are declaring the selector twice. chain method only allows you to not declare the selector again and again.you can bind as many method as you want using chain method.imagine you want hover method for same selector. traditionally,you'll declare the 3 methods with same selector thrice in them.using chain method:

$("#linka").click(function() {

 // method1

}).fancybox({

 // method2

 }).hover(function(){//method3
 },function(){
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.