0

Here is the first bit:

<script type='text/javascript'>
        window.onload=function(){
        var strLinks = '';

        for (var i = 1; i <= 23; i++) {
        strLinks += '<li><a><img src="chapter_1/'+ i +'.jpg" alt="" /></a></li>';
    }   

        document.getElementById("pages").innerHTML = strLinks;
    }
    </script>

And the second bit:

<script type='text/javascript'>
    window.onload=function(){
    var strLinks = '';

    for (var i = 1; i <= 5; i++) {
    strLinks += '<option value="http://google.com">link '+ i +'</option>';
}   

    document.getElementById("chapter").innerHTML = strLinks;
}
</script>

The first one would basically add images automatically and the second one would add links automatically as well until the loop ends. However when I try to put these two together, the first one won't run at all. How do I get both of these to run?

Nari
  • 41
  • 4

2 Answers2

7

Use addEventListener instead of assigning (and overwriting) the property directly.

addEventListener('load', myFunction);
addEventListener('load', myOtherFunction);

function myFunction(event) {
  document.body.appendChild(
    document.createTextNode("some text")
  );
}

function myOtherFunction(event) {
  document.body.appendChild(
    document.createTextNode("some other text")
  );
}
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
-2

You can only have one onload function. Therefore, do the following:

<script type='text/javascript'>
window.onload=function(){
    var strLinks = '';

    for (var i = 1; i <= 23; i++) {
        strLinks += '<li><a><img src="chapter_1/'+ i +'.jpg" alt="" /></a></li>';
    }   

    document.getElementById("pages").innerHTML = strLinks;

    strLinks = '';

    for (var i = 1; i <= 5; i++) {
        strLinks += '<option value="http://google.com">link '+ i +'</option>';
    }   

    document.getElementById("chapter").innerHTML = strLinks;
}
</script>

Also, please remove magic numbers like '23' and '5'. User variables to define these values.

Anonymous0day
  • 2,662
  • 1
  • 11
  • 15
Mate Hegedus
  • 2,628
  • 1
  • 17
  • 29
  • Thank you! I'm fairly new to this so I don't completely understand yet the lingo used in the other answers. – Nari Oct 09 '15 at 14:32