Search code examples
javascripthtmlnode.jsexpresshtmlelements

HOW TO LOOP HTML ELEMENTS WITHOUT USING forEach()?


I'm currently working with my web based system using nodejs and html.

Is there any alternative way to loop html elements without using forEach?

I'm thinking about like this(this is just an example):

 <% for(var ctr=0; ctr<arrayname.length;ctr++){ %>`
      `<input type="text">`
    `<%}%>`

I hope someone will answer my question. :)


Solution

  • It seems that your issue is that you need to be access your iterating variable, which is workable through a .forEach call as the second argument in the function provided to the .forEach call.

    Code below to show proof.

    $(function(){
      var items = ['one','two','three'];
      items.forEach(function(item,ctr){
        $('#testing').append('<div class="ctr-'+ctr+'">'+item+' CTR: '+ctr+'</div>');
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="testing">
    </div>