Jump to content

Sort list upon loading page instead of button click

Tim Jordan

Recommended Posts

Hi everyone,

I've been trying to look into getting a function to run whenever a page loads instead of having it happen on button click.

To be more specific, I have an unordered list that I wanted sorted Alphabetically when it is loaded so there doesn't have to be a sort button on the top of the page. The page is an article inside of a main page and inside that article section is where the list is contained. I have tried the script from here: https://www.w3schools.com/howto/howto_js_sort_list.asp and it works when i have a button and click, but when I try to get it to run automatically it doesn't work. I have tried:

just simply replacing the button line with <script>sortlist()</script> both above and below the list code

using onload with inside the article element and ul elements to get it to execute

Please let me know how this would be possible.

Link to comment
Share on other sites

The page is read top to bottom, everything referred to must exist and be fully rendered.

HTML with referred to id, class or possibly just tags such as ul, li.

Defining of function with references, to class, id etc.

Calling of function that must be defined before being called.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...