-1

I would like to ask on how I can use both functions once the page loads

jQuery(document).ready(function($)
{
    $('#list').tableScroll({height:500});

});

and

jQuery(document).ready(function($)
{
    $('#list').tableSorter();

});
1
  • 1
    It's not the shortest way to write it, but your two separate pieces of code would work if you put them both in the page. Commented Apr 3, 2013 at 12:03

9 Answers 9

7
jQuery(document).ready(function($) {
    $('#list').tableSorter().tableScroll({height:500});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Much better than my answer :)
3

jQuery supports method chaining.

jQuery(document).ready(function($) {
    $('#list')
        .tableScroll({height:500})
        .tableSorter();    
});

1 Comment

You're right.. this is true only if the plugins you are using supports chainability
1
jQuery(document).ready(function($)
{
    $('#list').tableScroll({height:500});
    $('#list').tableSorter();
});

Comments

1

Just put both under one DOM ready handler and use chaining:

$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});

Comments

0
$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});

Comments

0

I guess its fine to have more than one

jQuery(document).ready(function($) { .... }

both will be called on page on load body :). irrespective of no of call`s made, all will be called on page load only.

Comments

0

Simple, use

jQuery(document).ready(function() {
    $('#list').tableScroll({height:500}).tableSorter();
});

Comments

0

There is a shorter version of jQuery(document).ready(function()) that you could use that would have the same result:

 $(function() {
   // code to execute when the DOM is ready
 });

For this situation, using the elegant chaining:

$(function() {
    $('#list').tableSorter().tableScroll({height:500});
 });

For a discussion of the difference between these two approaches, see this very helpful question.

3 Comments

but maybe toink uses other libraries so that $ has to be escaped?
Well, @elektronikLexikon, there is always the possibility of collisions when multiple libraries are used, but that is not the question here.
it could be the reason why he used jQuery(document).ready(function($) in his question, though.
0

Here's how I would do it:

// Create an immediately-invoked function expression
(function ($) {

    // Enable strict mode
    "use strict";

    // Cache the selector so the script
    // only searches the DOM once
    var myList = $('#list'); 

    // Chain the methods together
    myList.tableScroll({height:500}).tableSorter();

}(jQuery));

Writing your jQuery in an IIFE like this means you can run the code alongside other libraries that also use $, and you won’t get conflicts.

Be sure to include this JavaScript at the end of your document, just before the closing </body> tag.

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.