Skip to main content
update narrative
Source Link

The below function is used to create multiple divscontent division elements (i.e. <div>) inside a containing div<div>. The function is working correctly and is as expected, I just feel the quality of the code itself is a bit poor.

Therefore, can the following function be, in any way, simplified or improved?

generate(chartData) {
      var container = document.createElement("div");
      $(container).attr("class", "container2");

      var myNode = document.getElementById(chartData.bindto.replace("#", "")).appendChild(container);

      for(var i = 0; i < 10; i++) {
        var div = document.createElement('div');
        $(div).attr("class", "block");
        var span = document.createElement('span');
        var text = "Hello World!";
        span.append(text);
        div.append(span);
        myNode.append(div);
      }
    }

The below function is used to create multiple divs inside a containing div. The function is working correctly and is as expected, I just feel the quality of the code itself is a bit poor.

Therefore, can the following function be, in any way, simplified or improved?

generate(chartData) {
      var container = document.createElement("div");
      $(container).attr("class", "container2");

      var myNode = document.getElementById(chartData.bindto.replace("#", "")).appendChild(container);

      for(var i = 0; i < 10; i++) {
        var div = document.createElement('div');
        $(div).attr("class", "block");
        var span = document.createElement('span');
        var text = "Hello World!";
        span.append(text);
        div.append(span);
        myNode.append(div);
      }
    }

The below function is used to create multiple content division elements (i.e. <div>) inside a containing <div>. The function is working correctly and is as expected, I just feel the quality of the code itself is a bit poor.

Therefore, can the following function be, in any way, simplified or improved?

generate(chartData) {
      var container = document.createElement("div");
      $(container).attr("class", "container2");

      var myNode = document.getElementById(chartData.bindto.replace("#", "")).appendChild(container);

      for(var i = 0; i < 10; i++) {
        var div = document.createElement('div');
        $(div).attr("class", "block");
        var span = document.createElement('span');
        var text = "Hello World!";
        span.append(text);
        div.append(span);
        myNode.append(div);
      }
    }
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
edited title
Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155

Can the following Javascript be simplified or improved? Create multiple divs inside a containing div

Source Link
Loading