0

I'm trying to get my javascript function that creates a html table in my content div on my website. The problem is, that it is not displayed inside the div but under it. How can I put it inside?

<div id="tab-container" class='tab-container'>
    <ul class='etabs'>
        <li class='tab'><a href="#tabs1-start">Start</a></li>
        <li class='tab'><a href="#tabs1-schicht">Schichtplan</a></li>
        <li class='tab'><a href="#tabs1-rechnung">Rechnung</a></li>
        <li class='tab'><a href="#tabs1-kontakt">Kontakt</a></li>
    </ul>
    <div class='panel-container'>
        <div id="tabs1-start">
            Test page 1
        </div>
        <div id="tabs1-schicht">
            <script type"text/javascript">
                tableCreate();
            </script>
        </div>
        <div id="tabs1-rechnung">
            Test page3

        </div>
        <div id="tabs1-kontakt">
            Test page4
        </div>
    </div>
</div>

This is my basic website with the navigationbar. When I run it with xampp the js function tableCreate(); is displayed after the panel-container div closes.

Here is my js code:

function tableCreate(){
//var schicht=["datum","schicht","schicht","schicht","dispo","schicht","schicht","schicht","schicht","schicht","schicht","dispo"];
var body=document.getElementsByTagName('body')[0];
var tbl=document.createElement('table');
tbl.style.width='100%';
tbl.setAttribute('border','1');
var tbdy=document.createElement('tbody');
for(var i=0;i<28;i++){
    var tr=document.createElement('tr');
    for(var j=0;j<12;j++){
        if(i==27 && j==12){
                break   
                 } else {
        var td=document.createElement('td');
        td.appendChild(document.createTextNode('schicht'))
        tr.appendChild(td)
        }
    }
    tbdy.appendChild(tr);
}
tbl.appendChild(tbdy);
body.appendChild(tbl)

Thanks in advance.

5
  • Provide demo please (jsfiddle for example). Commented Dec 10, 2014 at 7:07
  • You are appending the table to the body so you need to append it to the div with the id of tabs1-schicht Commented Dec 10, 2014 at 7:10
  • complex js like these might not work on all browsers. i suggest you use a js library like jquery. Commented Dec 10, 2014 at 7:44
  • complex js like these might not work on all browsers. i suggest you use a js library like jquery. Commented Dec 10, 2014 at 7:44
  • @astro I looked up your suggestion. I changed my table creation to something simular to stackoverflow.com/a/1413984/4344444. It works now. thanks a lot =D Commented Dec 10, 2014 at 13:40

4 Answers 4

1

Use document.getElementById('tabs1-schicht'); instead of document.getElementsByTagName('body')[0];

Sign up to request clarification or add additional context in comments.

4 Comments

I changed the code. Now it's not displayed anymore.
Probably because your js code is missing closing brace for the function. Inspect your code to see if there are any JavaScript errors.
Actually is it good to execute it as a function? Or is it better to just implement the script on top of the html page. Because don't I say in the js file where I want to put the table?
using functions make your code much neater. this code `document.getElementById('tabs1-schicht');' is telling you js where to put the table.
0

Replace

body.appendChild(tbl)

with

var div4=document.getElementById("tabs1-kontakt");
div4.appendChild(tbl);

Comments

0

You code should be something like this:

<html>
        <head>
            <script>
                function tableCreate() {
                    //var schicht=["datum","schicht","schicht","schicht","dispo","schicht","schicht","schicht","schicht","schicht","schicht","dispo"];
                    var body = document.getElementById('tabs1-schicht');
                    var tbl = document.createElement('table');
                    tbl.style.width = '100%';
                    tbl.setAttribute('border', '1');
                    var tbdy = document.createElement('tbody');
                    for (var i = 0; i < 28; i++) {
                        var tr = document.createElement('tr');
                        for (var j = 0; j < 12; j++) {
                            if (i == 27 && j == 12) {
                                break
                            } else {
                                var td = document.createElement('td');
                                td.appendChild(document.createTextNode('schicht'))
                                tr.appendChild(td)
                            }
                        }
                        tbdy.appendChild(tr);
                    }
                    tbl.appendChild(tbdy);
                    body.appendChild(tbl)
                }
            </script>
        </head>
        <body>
            <div id="tab-container" class='tab-container'>
                <ul class='etabs'>
                    <li class='tab'><a href="#tabs1-start">Start</a></li>
                    <li class='tab'><a href="#tabs1-schicht">Schichtplan</a></li>
                    <li class='tab'><a href="#tabs1-rechnung">Rechnung</a></li>
                    <li class='tab'><a href="#tabs1-kontakt">Kontakt</a></li>
                </ul>
                <div class='panel-container'>
                    <div id="tabs1-start">
                        Test page 1
                    </div>
                    <div id="tabs1-schicht">
                        <script>
                            tableCreate();
                        </script>
                    </div>
                    <div id="tabs1-rechnung">
                        Test page3

                    </div>
                    <div id="tabs1-kontakt">
                        Test page4
                    </div>
                </div>
            </div>
        </body>
    </html>

2 Comments

I tried it like this. in jsfiddle and on xampp. There is no output.
just copy the whole code as is and save it as html document. It should work.
0

@astro I looked up your suggestion. I changed my table creation to something simular to stackoverflow.com/a/1413984/4344444. It works now. thanks a lot =D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.