PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

[FIXED] Why won't my JavaScript load on my webpage (using window.onload)?

 July 26, 2022     arrays, dreamweaver, html, javascript, onload     No comments   

Issue

I'm trying to develop an HTML Page within Adobe Dreamweaver using the Live Preview function (where I view the webpage on a local server) and my JavaScript is being finicky. Im trying to populate a dropdown menu with a JS array and my previous testing worked, but now when I try to get the script to run automatically, either a) nothing happens or b) the button flashes suggesting it was updated but still shows blank. Any help would be appreciated!

    <script>
    function start() {

        var arrOptions = []; //arrOptionsCollection with HTML tags
        var arrOptionsCollection = [0, 1, 2]; //Values for dropdown

        arrOptions.push("<option value='Select From List...'>Select From List...</option>") //Dropdown Default


        //Adds HTML tags to arrOptionsCollections
        for (var i=0, n = arrOptionsCollection.length; i < n; i++) { =
            arrOptions.push("<option value='" + arrOptionsCollection[i] + "'>" + arrOptionsCollection[i] + "</option>");
        }

        //Transfers arrOptions to actual HTML
        document.getElementById("district-select").innerHTML = arrOptions.join();
    }

    document.getElementById("form-select").onload = function() {start()};

</script>

<body onload="start();">
<form id="form-select">
    <select id="district-select">
        <!--Script Inserts Options Here-->
    </select>

</form>

Solution

I wonder what's the role of the = at the beginning of your for loop. Simply, it doesn't make any sense.

So instead of:

for (var i=0, n = arrOptionsCollection.length; i < n; i++) { =

Put:

for (var i=0, n = arrOptionsCollection.length; i < n; i++) {

Here is your code:

function start() {

        var arrOptions = []; //arrOptionsCollection with HTML tags
        var arrOptionsCollection = [0, 1, 2]; //Values for dropdown

        arrOptions.push("<option value='Select From List...'>Select From List...</option>") //Dropdown Default


        //Adds HTML tags to arrOptionsCollections
        for (var i=0, n = arrOptionsCollection.length; i < n; i++) {
            arrOptions.push("<option value='" + arrOptionsCollection[i] + "'>" + arrOptionsCollection[i] + "</option>");
        }

        //Transfers arrOptions to actual HTML
        document.getElementById("district-select").innerHTML = arrOptions.join();
    }

    document.getElementById("form-select").onload = function() {start()};
<body onload="start();">
<form id="form-select">
    <select id="district-select">
        <!--Script Inserts Options Here-->
    </select>

</form>

Here is a living demo: https://codepen.io/marchmello/pen/yLYbYjw?editors=1010



Answered By - MARSHMALLOW
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing