2

i have array data it call "provinsi" i got array on console like normal array

here's my code

 <select id="select">
      <option value="default">default</option>
 </select>
 <script>
        console.log(provinsi)
        var select = document.getElementById("select");

        for(var i = 0; i < provinsi.length; i++)
        {
            var option = document.createElement("option"),
                txt = document.createTextNode(provinsi[i]);
            option.appendChild(txt);
            option.setAttribute("value",provinsi[i]);
            select.insertBefore(option,select.lastChild);
        }
    </script>

but i got result like this enter image description here

here's my console "provinsi" enter image description here any suggest ? thanks before

6
  • 1
    what is the expected result? Commented Jul 24, 2018 at 16:04
  • sorry forgot about that, i need select option data is my array it call "provinsi" Commented Jul 24, 2018 at 16:07
  • jsfiddle.net/w83b7j6x/1 - seems to work for me. Where are you defining provinsi? Commented Jul 24, 2018 at 16:08
  • i use on blade.php laravel , i defining from leaflet, and i use the code to bind data to option in blade inside tag <script> Commented Jul 24, 2018 at 16:10
  • 2
    The code by itself is working fine. Can you show how you are populating the variable provinsi Commented Jul 24, 2018 at 16:10

4 Answers 4

2

I believe the DOM is not ready yet.

Try putting your code inside IIFE.

(function() {
    // the DOM will be available here

    console.log(provinsi)
    var select = document.getElementById("select");

    for(var i = 0; i < provinsi.length; i++)
    {
        var option = document.createElement("option"),
            txt = document.createTextNode(provinsi[i]);
        option.appendChild(txt);
        option.setAttribute("value",provinsi[i]);
        select.insertBefore(option,select.lastChild);
    }

})();

** Just to clarify as @karthick mentioned, make sure the script is loaded at the end of the body tag.

Sign up to request clarification or add additional context in comments.

2 Comments

you need to use DOMContentLoaded to tell dom is ready not iife
@karthick I don't must anything. you can do that in iife as long as the script is loaded at the end of the body tag. Edit: Actually, This is faster to execute than an onload handler because this waits only for the DOM to be ready, not for all images to load. And, this works in every browser.
1

I hope this will work happy coding :) Note: change your provinces array with orginal values

$(document).ready(()=> {
  let provinces = ["test1", "test2", "test3", "test4" ]
  $.each(provinces, function(key,item) {
  $("#select").append(new Option(item, key));
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select">
      <option value="default">default</option>
 </select>

Comments

0

The DOM may not have finished loading when your script is run. You should run your function on DOMContentLoaded.

<select id="select">
      <option value="default">default</option>
 </select>
 <script>
  var provinsi = ["test1", "test2"];
        document.addEventListener("DOMContentLoaded", function(event) {
    //DOM fully loaded and parsed
    var select = document.getElementById("select");

        for(var i = 0; i < provinsi.length; i++)
        {
            var option = document.createElement("option"),
                txt = document.createTextNode(provinsi[i]);
            option.appendChild(txt);
            option.setAttribute("value",provinsi[i]);
            select.insertBefore(option,select.lastChild);
        }
  });
        
    </script>
<script>

</script>

Comments

0

You can use jquery each function for looping

Javascript Code:

var makers = [
  {id: 1, name: 'Toyota'}, 
  {id: 2, name: 'Nissan'}, 
  {id: 3, name: 'Honda'}
]
    
$.each(makers, function (index, item) {
   $('#makers-listing').append(`<option value="${item.id}">${item.name}</option>`);
});

Html Code:

<select id="makers-listing">
    <option>Select Maker</option> <!-- This is default value -->
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.