HTML Table is not sized correctly after adding items with js












2















I create a table with this HTML Code:



<div class="noten_tabelle">
<table id="grades_table" style="width:100%">
<tr>
<th>Fach</th>
<th>mündlich</th>
<th>Klausur</th>
</tr>
<!-- Make content with js code -->
</table>
</div>


And in Javascript I add this things to the table. The function runs every time the server returns new loaded values. Here is the Javascript function:



function addToTable(subject, mdl, klu) {

var subject_name = getSubjectByNumber(subject);

//Zeile erstellen

var y = document.createElement([subject_name]);
y.setAttribute("id", [subject_name]);
document.getElementById("grades_table").appendChild(y);

//Spalten in einer Zeile

var cE = document.createElement("TD");
var tE = document.createTextNode([subject_name]);
cE.appendChild(tE);
document.getElementById([subject_name]).appendChild(cE);

var a = document.createElement("TD");
var b = document.createTextNode([mdl]);
a.appendChild(b);
document.getElementById([subject_name]).appendChild(a);

var c = document.createElement("TD");
var d = document.createTextNode([klu]);
c.appendChild(d);
document.getElementById([subject_name]).appendChild(c);
}


For understanding. The subject value is a number, so I turn it into a String. The result is the subject name. He is something like: "Mathematik" or "Deutsch".

I am getting this:



table



But that's not correct. The subject_name "Latein" is correct under the "Fach", but "5,8" should be under "mündlich". And the "11,4" should be under "klausur".

What's wrong because the "Vokalensemle should be in the next line again under "Fach", the "4,7" under "mündlich" and the empty field under "Klausur".



Oh yeah. And here is my CSS:



table {
font-family: Montserrat-Medium;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}


I hope you find a solution to my Problem. If you need anything to know let me know.










share|improve this question

























  • You are inputing only <td>.. You didn't mind <tr>.

    – Banujan Balendrakumar
    Nov 22 '18 at 17:00
















2















I create a table with this HTML Code:



<div class="noten_tabelle">
<table id="grades_table" style="width:100%">
<tr>
<th>Fach</th>
<th>mündlich</th>
<th>Klausur</th>
</tr>
<!-- Make content with js code -->
</table>
</div>


And in Javascript I add this things to the table. The function runs every time the server returns new loaded values. Here is the Javascript function:



function addToTable(subject, mdl, klu) {

var subject_name = getSubjectByNumber(subject);

//Zeile erstellen

var y = document.createElement([subject_name]);
y.setAttribute("id", [subject_name]);
document.getElementById("grades_table").appendChild(y);

//Spalten in einer Zeile

var cE = document.createElement("TD");
var tE = document.createTextNode([subject_name]);
cE.appendChild(tE);
document.getElementById([subject_name]).appendChild(cE);

var a = document.createElement("TD");
var b = document.createTextNode([mdl]);
a.appendChild(b);
document.getElementById([subject_name]).appendChild(a);

var c = document.createElement("TD");
var d = document.createTextNode([klu]);
c.appendChild(d);
document.getElementById([subject_name]).appendChild(c);
}


For understanding. The subject value is a number, so I turn it into a String. The result is the subject name. He is something like: "Mathematik" or "Deutsch".

I am getting this:



table



But that's not correct. The subject_name "Latein" is correct under the "Fach", but "5,8" should be under "mündlich". And the "11,4" should be under "klausur".

What's wrong because the "Vokalensemle should be in the next line again under "Fach", the "4,7" under "mündlich" and the empty field under "Klausur".



Oh yeah. And here is my CSS:



table {
font-family: Montserrat-Medium;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}


I hope you find a solution to my Problem. If you need anything to know let me know.










share|improve this question

























  • You are inputing only <td>.. You didn't mind <tr>.

    – Banujan Balendrakumar
    Nov 22 '18 at 17:00














2












2








2








I create a table with this HTML Code:



<div class="noten_tabelle">
<table id="grades_table" style="width:100%">
<tr>
<th>Fach</th>
<th>mündlich</th>
<th>Klausur</th>
</tr>
<!-- Make content with js code -->
</table>
</div>


And in Javascript I add this things to the table. The function runs every time the server returns new loaded values. Here is the Javascript function:



function addToTable(subject, mdl, klu) {

var subject_name = getSubjectByNumber(subject);

//Zeile erstellen

var y = document.createElement([subject_name]);
y.setAttribute("id", [subject_name]);
document.getElementById("grades_table").appendChild(y);

//Spalten in einer Zeile

var cE = document.createElement("TD");
var tE = document.createTextNode([subject_name]);
cE.appendChild(tE);
document.getElementById([subject_name]).appendChild(cE);

var a = document.createElement("TD");
var b = document.createTextNode([mdl]);
a.appendChild(b);
document.getElementById([subject_name]).appendChild(a);

var c = document.createElement("TD");
var d = document.createTextNode([klu]);
c.appendChild(d);
document.getElementById([subject_name]).appendChild(c);
}


For understanding. The subject value is a number, so I turn it into a String. The result is the subject name. He is something like: "Mathematik" or "Deutsch".

I am getting this:



table



But that's not correct. The subject_name "Latein" is correct under the "Fach", but "5,8" should be under "mündlich". And the "11,4" should be under "klausur".

What's wrong because the "Vokalensemle should be in the next line again under "Fach", the "4,7" under "mündlich" and the empty field under "Klausur".



Oh yeah. And here is my CSS:



table {
font-family: Montserrat-Medium;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}


I hope you find a solution to my Problem. If you need anything to know let me know.










share|improve this question
















I create a table with this HTML Code:



<div class="noten_tabelle">
<table id="grades_table" style="width:100%">
<tr>
<th>Fach</th>
<th>mündlich</th>
<th>Klausur</th>
</tr>
<!-- Make content with js code -->
</table>
</div>


And in Javascript I add this things to the table. The function runs every time the server returns new loaded values. Here is the Javascript function:



function addToTable(subject, mdl, klu) {

var subject_name = getSubjectByNumber(subject);

//Zeile erstellen

var y = document.createElement([subject_name]);
y.setAttribute("id", [subject_name]);
document.getElementById("grades_table").appendChild(y);

//Spalten in einer Zeile

var cE = document.createElement("TD");
var tE = document.createTextNode([subject_name]);
cE.appendChild(tE);
document.getElementById([subject_name]).appendChild(cE);

var a = document.createElement("TD");
var b = document.createTextNode([mdl]);
a.appendChild(b);
document.getElementById([subject_name]).appendChild(a);

var c = document.createElement("TD");
var d = document.createTextNode([klu]);
c.appendChild(d);
document.getElementById([subject_name]).appendChild(c);
}


For understanding. The subject value is a number, so I turn it into a String. The result is the subject name. He is something like: "Mathematik" or "Deutsch".

I am getting this:



table



But that's not correct. The subject_name "Latein" is correct under the "Fach", but "5,8" should be under "mündlich". And the "11,4" should be under "klausur".

What's wrong because the "Vokalensemle should be in the next line again under "Fach", the "4,7" under "mündlich" and the empty field under "Klausur".



Oh yeah. And here is my CSS:



table {
font-family: Montserrat-Medium;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}


I hope you find a solution to my Problem. If you need anything to know let me know.







javascript html css html-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 19:39









Brian Tompsett - 汤莱恩

4,2031338101




4,2031338101










asked Nov 22 '18 at 16:52









marcelo.wdrbmarcelo.wdrb

859




859













  • You are inputing only <td>.. You didn't mind <tr>.

    – Banujan Balendrakumar
    Nov 22 '18 at 17:00



















  • You are inputing only <td>.. You didn't mind <tr>.

    – Banujan Balendrakumar
    Nov 22 '18 at 17:00

















You are inputing only <td>.. You didn't mind <tr>.

– Banujan Balendrakumar
Nov 22 '18 at 17:00





You are inputing only <td>.. You didn't mind <tr>.

– Banujan Balendrakumar
Nov 22 '18 at 17:00












2 Answers
2






active

oldest

votes


















1














      var y = document.createElement("TR");
y.setAttribute("id", [subject_name]);

//Spalten in einer Zeile

var cE = document.createElement("TD");
var tE = document.createTextNode([subject_name]);
cE.appendChild(tE);
y.appendChild(cE);

var a = document.createElement("TD");
var b = document.createTextNode([mdl]);
a.appendChild(b);
y.appendChild(a);

var c = document.createElement("TD");
var d = document.createTextNode([klu]);
c.appendChild(d);
y.appendChild(c);


document.getElementById("grades_table").appendChild(y);





share|improve this answer































    0














    I couldn't see in your code where you are adding the new parent table row for your new table cells.



    The cells will align if they are within a new row, with the same number of table cells.



    Append a new tr and add the new td cells to that.



    If you need to have uneven number of cells per row, you will need to use colspan={number of columns cell will span over}.



    Would you like a code example?






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435404%2fhtml-table-is-not-sized-correctly-after-adding-items-with-js%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














            var y = document.createElement("TR");
      y.setAttribute("id", [subject_name]);

      //Spalten in einer Zeile

      var cE = document.createElement("TD");
      var tE = document.createTextNode([subject_name]);
      cE.appendChild(tE);
      y.appendChild(cE);

      var a = document.createElement("TD");
      var b = document.createTextNode([mdl]);
      a.appendChild(b);
      y.appendChild(a);

      var c = document.createElement("TD");
      var d = document.createTextNode([klu]);
      c.appendChild(d);
      y.appendChild(c);


      document.getElementById("grades_table").appendChild(y);





      share|improve this answer




























        1














              var y = document.createElement("TR");
        y.setAttribute("id", [subject_name]);

        //Spalten in einer Zeile

        var cE = document.createElement("TD");
        var tE = document.createTextNode([subject_name]);
        cE.appendChild(tE);
        y.appendChild(cE);

        var a = document.createElement("TD");
        var b = document.createTextNode([mdl]);
        a.appendChild(b);
        y.appendChild(a);

        var c = document.createElement("TD");
        var d = document.createTextNode([klu]);
        c.appendChild(d);
        y.appendChild(c);


        document.getElementById("grades_table").appendChild(y);





        share|improve this answer


























          1












          1








          1







                var y = document.createElement("TR");
          y.setAttribute("id", [subject_name]);

          //Spalten in einer Zeile

          var cE = document.createElement("TD");
          var tE = document.createTextNode([subject_name]);
          cE.appendChild(tE);
          y.appendChild(cE);

          var a = document.createElement("TD");
          var b = document.createTextNode([mdl]);
          a.appendChild(b);
          y.appendChild(a);

          var c = document.createElement("TD");
          var d = document.createTextNode([klu]);
          c.appendChild(d);
          y.appendChild(c);


          document.getElementById("grades_table").appendChild(y);





          share|improve this answer













                var y = document.createElement("TR");
          y.setAttribute("id", [subject_name]);

          //Spalten in einer Zeile

          var cE = document.createElement("TD");
          var tE = document.createTextNode([subject_name]);
          cE.appendChild(tE);
          y.appendChild(cE);

          var a = document.createElement("TD");
          var b = document.createTextNode([mdl]);
          a.appendChild(b);
          y.appendChild(a);

          var c = document.createElement("TD");
          var d = document.createTextNode([klu]);
          c.appendChild(d);
          y.appendChild(c);


          document.getElementById("grades_table").appendChild(y);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 17:03









          Banujan BalendrakumarBanujan Balendrakumar

          8391212




          8391212

























              0














              I couldn't see in your code where you are adding the new parent table row for your new table cells.



              The cells will align if they are within a new row, with the same number of table cells.



              Append a new tr and add the new td cells to that.



              If you need to have uneven number of cells per row, you will need to use colspan={number of columns cell will span over}.



              Would you like a code example?






              share|improve this answer




























                0














                I couldn't see in your code where you are adding the new parent table row for your new table cells.



                The cells will align if they are within a new row, with the same number of table cells.



                Append a new tr and add the new td cells to that.



                If you need to have uneven number of cells per row, you will need to use colspan={number of columns cell will span over}.



                Would you like a code example?






                share|improve this answer


























                  0












                  0








                  0







                  I couldn't see in your code where you are adding the new parent table row for your new table cells.



                  The cells will align if they are within a new row, with the same number of table cells.



                  Append a new tr and add the new td cells to that.



                  If you need to have uneven number of cells per row, you will need to use colspan={number of columns cell will span over}.



                  Would you like a code example?






                  share|improve this answer













                  I couldn't see in your code where you are adding the new parent table row for your new table cells.



                  The cells will align if they are within a new row, with the same number of table cells.



                  Append a new tr and add the new td cells to that.



                  If you need to have uneven number of cells per row, you will need to use colspan={number of columns cell will span over}.



                  Would you like a code example?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 17:00









                  BenHerbertBenHerbert

                  14029




                  14029






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435404%2fhtml-table-is-not-sized-correctly-after-adding-items-with-js%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen