user629034 user629034 - 1 year ago 42
HTML Question

auto sort the table using sorttable.js

I am trying to use this script to sort the html table:

JS code itself:

I used the customkey option and it works when I click the heading. I would like to auto sort based on that customkey when the page loads. How can I do that? I want to use this specific script. I tried window.onload but no luck.

Thanks for your help


After loading the page just trigger a click on the header column you want to sort. Try with following javascript code. I assume that there is only one table in the page. Perhaps you will need to be more specific using getElementsByTagName or with getElementById instead. The [1] is the column number. You don't need to attach a click handler before that because that task is done by the code of sorttable.js.

<script src="js/sorttable.js"></script>
    window.onload = function() {
        (document.getElementsByTagName( 'th' )[1]).click();