0

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

});
toink
  • 255
  • 3
  • 11
  • 30
  • 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. – Anthony Grist Apr 03 '13 at 12:03

9 Answers9

7
jQuery(document).ready(function($) {
    $('#list').tableSorter().tableScroll({height:500});
});
Flo Edelmann
  • 2,523
  • 1
  • 18
  • 33
3

jQuery supports method chaining.

jQuery(document).ready(function($) {
    $('#list')
        .tableScroll({height:500})
        .tableSorter();    
});
Daniel Imms
  • 45,529
  • 17
  • 143
  • 160
1
jQuery(document).ready(function($)
{
    $('#list').tableScroll({height:500});
    $('#list').tableSorter();
});
Mark Withers
  • 1,392
  • 4
  • 15
  • 33
1

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

$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});
VisioN
  • 138,460
  • 30
  • 271
  • 271
0
$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});
sasi
  • 3,886
  • 4
  • 26
  • 47
0

Simple, use

jQuery(document).ready(function() {
    $('#list').tableScroll({height:500}).tableSorter();
});
Jeff Shaver
  • 3,235
  • 17
  • 19
IndoKnight
  • 1,804
  • 1
  • 21
  • 29
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.

dreamweiver
  • 5,964
  • 2
  • 26
  • 39
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.

Community
  • 1
  • 1
DOK
  • 31,943
  • 7
  • 59
  • 92
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.

Jezen Thomas
  • 13,338
  • 6
  • 52
  • 90