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
  • 4
  • 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,573
  • 1
  • 20
  • 33
3

jQuery supports method chaining.

jQuery(document).ready(function($) {
    $('#list')
        .tableScroll({height:500})
        .tableSorter();    
});
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
1
jQuery(document).ready(function($)
{
    $('#list').tableScroll({height:500});
    $('#list').tableSorter();
});
Mark Withers
  • 1,462
  • 5
  • 16
  • 34
1

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

$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});
VisioN
  • 143,310
  • 32
  • 282
  • 281
0
$(document).ready(function() {
    $("#list").tableScroll({ height: 500 }).tableSorter();
});
sasi
  • 4,192
  • 4
  • 28
  • 47
0

Simple, use

jQuery(document).ready(function() {
    $('#list').tableScroll({height:500}).tableSorter();
});
Jeff Shaver
  • 3,315
  • 18
  • 19
IndoKnight
  • 1,846
  • 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
  • 6,002
  • 2
  • 24
  • 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
  • 32,337
  • 7
  • 60
  • 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,619
  • 6
  • 53
  • 91