0

enter image description hereHi i am trying to run a java script in nocode plateform. what i am actually trying to do is i am getting 2 dynamic values from dropdown and adding div according to those values respectively. one dropdown defines length and other one defines width. and i am trying to draw a grid by those values. i am able to add rows but can not add columns in 1st row.

`

<script>
window.onload = function () {
    var MainDiv = document.getElementById("Main");
    var virtualDiv= document.createElement('div');
    virtualDiv.id='Vdiv';
    var Rows = document.getElementById("rows");
    var Columns = document.getElementById("columns");
    var length;
    var width;
    var arrayDivRow = new Array();
    var arrayDivCol = new Array();
    
    
    
    if(Rows){
        Rows.addEventListener('change', function handleChange(event) {
         
        length = parseInt(event.target.value);
        console.log(length);
            for(var i=0;i<length;i++){
            arrayDivRow[i] = document.createElement('div');
            arrayDivRow[i].id='row'+i;
            arrayDivRow[i].style.backgroundColor = 'yellow';
            arrayDivRow[i].style.height = "100px";
            arrayDivRow[i].style.margin = "5px";
                if(Columns){
                    Columns.addEventListener('change', function                                                                 handleChange(event) {
        
                    width = parseInt(event.target.value);
                    console.log(width);
                        for(var j=0;j<width;j++){
                            arrayDivCol[j] = document.createElement('div');
                            arrayDivCol[j].id='col'+j;
                            arrayDivCol[j].style.backgroundColor = 'green';
                            arrayDivCol[j].style.height = "100px";
                            arrayDivCol[j].style.margin = "5px";
                            arrayDivRow[i].appendchild(arrayDivCol[j]);
                        } 
                        
                    });
                    
                }
                
            virtualDiv.appendChild(arrayDivRow[i]);
            } 
            
        });
    }
 MainDiv.appendChild(virtualDiv);
};

</script>


` here i am getting error

Uncaught TypeError: Cannot read properties of undefined (reading 'appendchild')
    at HTMLSelectElement.handleChange (<anonymous>:37:26)

in console of a browser

please help me solve this

  • Aside: `appendchild` doesn’t exist. You mean `appendChild`. – Sebastian Simon Dec 14 '22 at 12:34
  • What is unclear about an error message such as _“Cannot read property `appendchild` of `undefined`”_? You can’t read properties of `undefined`, but that’s what happens at `arrayDivRow[i].appendchild` because `arrayDivRow[i]` is `undefined`; it’s trying to get `appendchild` off of `undefined` — not possible. If you don’t know why `arrayDivRow[i]` is `undefined`, use the debugger or look at intermediate values: `console.log(i);`. The solution would be to not use `var` or not use `for` loops; preferably both. – Sebastian Simon Dec 14 '22 at 12:36
  • Use [event delegation](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of adding several event listeners — it’s more maintainable and applies to dynamically added elements. See [the tag info](/tags/event-delegation/info) and [this Q&A](/a/34896387/4642212). – Sebastian Simon Dec 14 '22 at 12:38
  • Aside: appendchild doesn’t exist. You mean appendChild---this was the proble thank you so much sir :) – user2239404 Dec 14 '22 at 12:44
  • This wasn’t _the_ problem. It was _a_ problem. The spelling of `appendchild` does not produce the `TypeError` you mentioned in your question. – Sebastian Simon Dec 14 '22 at 12:52

0 Answers0