1

I have

<div id="tablePlace"></div>

And

if ($('#radioOne').is(':checked') == true) {
         $("#tablePlace").html(" ");
         $("#tablePlace").append(htmlTable); //htmlTable is a string that contains an html table code
         loadNestedTable(temp);
    }

It works but in the div I find NaN. If I comment $("#tablePlace").append(htmlTable);, NaN doesn't appear.

Why?

UPDATE

htmlValue code:

var tab = '<table id="decretoSingolo">'+
+'<thead>' 
+   '<tr>'
+       '<th>Ente</th>'
+       '<th>CUP</th>'
+       '<th>Decreto impegno</th>'
+       '<th>Data decreto impegno</th>'
+       '<th>Importo impegno</th>'
+       '<th>Finanziato MIUR</th>'
+       '<th>Importo pagato</th>'
+       '<th>Importo in pagamento</th>'
+   '</tr>'
+ '</thead>'
+   '<tbody>'
+   '</tbody>'
+'</table>'

+'<div style="display:none">'    
+   '<table id="dettagliDecretoSingolo">'
+   '<thead>' 
+           '<tr>'
+               '<th>Progressivo pagamento</th>'
+               '<th>Data decreto</th>'
+               '<th>Numero decreto pagamento</th>'
+               '<th>Tipo pagamento</th>'
+               '<th>Importo in pagamento</th>'
+               '<th>Nota decreto</th>'
+           '</tr>'
+       '</thead>'
+       '<tbody>'
+       '</tbody>'
+   '</table>'
+'</div>';

htmlTable value:

<table id="myTable">NaN<tr><th>Ente</th><th>CUP</th><th>Decreto impegno</th><th>Data decreto impegno</th><th>Importo impegno</th><th>Finanziato</th><th>Importo pagato</th><th>Importo in pagamento</th></tr></thead><tbody></tbody></table><div style="display:none"><table id="myTableDetails"><thead><tr><th>Progressivo pagamento</th><th>Data decreto</th><th>Numero decreto pagamento</th><th>Tipo pagamento</th><th>Importo</th><th>Nota</th></tr></thead><tbody></tbody></table></div>

NaN appears after .append(). There is a problem in the htmlTable code?

9
  • console.log the value of htmlTable variable Commented Jan 7, 2016 at 10:19
  • 3
    What is the value of htmlTable? Commented Jan 7, 2016 at 10:19
  • 1
    @AnoopJoshi: Or better yet, use the debugger and inspect it as the code runs. Commented Jan 7, 2016 at 10:20
  • htmlTable is not numeric value but could you please post from where it comes? Commented Jan 7, 2016 at 10:22
  • 2
    You have NaN in your html how you expect from it to just disapear. Commented Jan 7, 2016 at 10:23

3 Answers 3

6

The problem is that you have a unary + in your code:

   var tab = '<table id="decretoSingolo">'+
   +'<thead>' 
// ^--- Here

To fix it:

Remove one of the +s. Usually it's best to use the + at the end of the previous line, to avoid issues with automatic semicolon insertion.

Why you're getting NaN:

It's a unary + because it follows the + at the end of the previous line, with whitespace in-between them (so it's not ++ as I initially suggested).

That unary + will try to take its operand (the string that follows it) and convert it to a number, and if that can't be done will yield NaN. Then the operands to the + on the previous line are a string and a number, so that addition operator converts the string to number and adds it to NaN (which yields NaN).

You can see it here:

var tab = '<table id="decretoSingolo">'+
+'<thead>' 
+   '<tr>';
document.body.innerHTML = tab;


Side note: There's no need to do .html(" ") and then .append(htmlTable), just do .html(htmlTable).

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

1 Comment

I'm very agree with "Usually it's best to use the + at the end of the previous line" . ^^
3

You have double plus sign + in the following lines, remove one :

var tabellaDecretoSingolo = '<table id="decretoSingolo">'+
+'<thead>' 
+   '<tr>'

Should be :

var tabellaDecretoSingolo = '<table id="decretoSingolo">'
+'<thead>' 
+   '<tr>'

Hope this helps.

3 Comments

why not directly $("#tablePlace").html(htmlTable); ?
Omg i had the same issue thank you very much, it made the trick
You're welcome, and I'm glad to see this answer helping after 3 years.
0

First, you can optimise your javascript:

if ($('#radioOne').is(':checked') == true) {
     $("#tablePlace").html(" ");
     $("#tablePlace").append(htmlTable); //htmlTable is a string that contains an html table code
     loadNestedTable(temp);
}

by

if ($('#radioOne').is(':checked') == true) {
     $("#tablePlace").html(htmlTable); //html() replace all content of your element child. 
     loadNestedTable(temp);
}

Also, you've a problem when you define your "htmlTable" value

<table id="myTable">NaN<tr> [...]

check after your ">" of your element if you don't add a NAN var...

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.