1

so, i'm trying to do a table in HTML and my code is like this.

<script>
function addtable()
{
 var incr;
 incr += "<table id="nicetable"....
 more code
 incr += "</table>";
 document.getElementById("divfortable").innerHTML=incr;
}
</script>
<div id="divfortable"></div>

And it appeared undefined and the table on my webpage. I've tried even commenting all of my code and only inserting a word in my "incr" variable, and still appeared the undefined word. So i did only:

document.getElementById("divfortable").innerHTML="hi"; 

And all went fine. And i've realized that the problem was from the var incr. And i don't know how to solve this. And it's quite odd, cuz I always do this to do tables, but now it's appearing the undefined word on the webpage.

3 Answers 3

2

You must escape the double quotes in incr, by replacing " with \". Javascript can use double quotes to represent the start and end of a string, but the quotes in id="nicetable" are interfering.

In addition, you should initialize your variable:

var incr = "";

As by default, incr will be set to undefined.

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

Comments

1
var incr;
 incr += "<table id=\"nicetable\"...."
 more code
 incr += "</table>";

or

var incr;
 incr += '<table id="nicetable"....'
 more code
 incr += '</table>';

1 Comment

Also, the variable incr should be defined like so: var incr = ""; to avoid a default string value of 'undefined'.
0

When you write this:

var incr;

Then incr has value undefined, then you try to append string on it, and it converts undefined to string and appends string on it.

Solution would be:

var incr = "<table id="nicetable"....

So you don't have undefined starting value of incr.

1 Comment

This is not incorrect, but his main issue lies with his quotations, check again. Also, to define a string variable without the "undefined" string do: var incr = "";

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.