title: d3 add text to SVG rect
body: <p>I'm beginner in d3.js and I want to add some text and rect inner a rect.</p>
  
  <p>here is my code:</p>
  
  <pre><code>            if (!_svg) {
              _svg = d3.select("#col1").append("svg")
                      .attr("height", _height - 2)
                      .attr("width", _width - 2);
          }
  
          _svg.append("text").attr({
              id: "leftDomainName",
              x: d3.select("svg").attr("width") / 14,
              y: d3.select("svg").attr("height") / 2,
              class: "vertical-text"
          }).text("Business Development");
  
          var g = _svg.append("g").attr("transform", function (d, i) {
              return "translate(0,0)";
          }).attr("clip-path", "url(#body-clip)");
  
  
          var mainBox = g.append("rect").attr({
              id: "mainBox",
              x: d3.select("svg").attr("width") / 8,
              y: 3,
              width: _width - 60,
              height: _height / 3,
              fill: "#D7FFEC"
          });
  
          mainBox.append("text").text("sample!!!");
          mainBox.append("rect").attr({
              x: 50,
              y: 50,
              width: _width - 60,
              height: _height / 3,
              fill: "red"
          });
  </code></pre>
  
  <p>I want to add a text and rect svg inner mainBox. In chrome develper tools I can see their was added but there aren't in page</p>
  
  <p><img src="https://i.stack.imgur.com/h82Im.jpg" alt="enter image description here"></p>
  
  <p>what is my wrong?
  thank you</p>
  
  <p><strong><em>I append g to mainBox and then append my element to that g but it didn't work</em></strong></p>
  
  <pre><code>            var innerG = mainBox.append("g");
  
          innerG.append("text").text("sample!!!");
          innerG.append("rect").attr({
              x: 50,
              y: 50,
              width: _width - 60,
              height: _height / 3,
              fill: "red"
          });
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/niKwG.jpg" alt="enter image description here"></p>