Home > Enterprise >  How to display array data inside textbox using loop? (One textbox per array data)
How to display array data inside textbox using loop? (One textbox per array data)

Time:11-19

Good day! I badly need help for this one, I have an array with many elements/data that is needed to be display in textbox. Each array element/data must be inside the textbox. (The Textbox must be dynamically set up using loop with the array data inside it)


                    
                        arr = ["1"-"2"-"3"-"4"-"5"]; //my array is from the db, this is example only
                        conv_arr = arr.split("-")
                        
                        var myArray = [conv_arr];
                        
                        var ArrayInText = document.createElement('input');

                        myArray.forEach(function(conv_arr) {

                        ArrayInText.value = conv_arr ;
                        
                        document.body.appendChild(ArrayInText);

It displays the array (pretend this is a textbox [ ]) [ 1, 2, 3, 4, 5 ]

I want a result that looks like this (One textbox per element using loop) [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

CodePudding user response:

You can see the demo here=> https://jsfiddle.net/4ow6k8j5/1/

After removing unnecessary assignments, you can use below simplest solution;

conv_arr = arr.split("-")

conv_arr.forEach(function(elem) {
  var ArrayInText = document.createElement('input');
  ArrayInText.value =  elem ;
  document.body.appendChild(ArrayInText);
});
  • Related