12th Aug 2017, 1:28 PM
ARB
ARB - avatar
5 odpowiedzi
+ 3
12th Aug 2017, 1:44 PM
Raz
Raz - avatar
+ 2
When the script loads the HTMl content is not loaded yet so it returns undefined or null you can use window.setTimeout(function,Time in milliseconds) or put a script at the end of the document calling a function on the main script to start the whole script. hope it helped.I tried to fix it, link:https://code.sololearn.com/WgQS6CHCCihW/#js
12th Aug 2017, 1:43 PM
Fast Login
Fast Login - avatar
+ 2
do you want to sort by name or year after clicking on the button?
12th Aug 2017, 2:38 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 2
yes @nomeh
12th Aug 2017, 3:45 PM
ARB
ARB - avatar
+ 1
All you need to do its wrap the first call to displayCars in a window.onload function as @neicore has done or you can use something like the jQuery document.ready function. The problem is that displayCars() is being called prior to the body of the html being built and added to the DOM. SL injects the JS from the JS tab into the head of the html so it is run first. Of course, you won't notice that it is actually working correctly (except the errors will be gone) until you change the order of your cars variable as they are already sorted in the order that the sort method would sort them.
12th Aug 2017, 3:58 PM
ChaoticDawg
ChaoticDawg - avatar