jquery/js error - DOM Error: DOM Exception 8 - When I .html any variable

8k Views Asked by At

I'm receiving a DOM Error: DOM Exception 8 when I try to execute a very simple piece of code. Ive done similar executions a million times and I'm very confused what the problem is.

My Code:

$.post('/dataHandlers/return_stats.php', function(data) {
       $('#u').html(data['username']);
       var health = data['health'];
       $('#health_display').html(health);
       $('#energy_display').html(data['energy']);
       $('#money_display').html(data['money']);
       $('#intelligence_display').html(data['intelligence']);
       $('#strength_display').html(data['strength']);
       $('#sidebar').find('#level_display').html(data['level']);
   }, 'json');

HTML:

<div id='sidebar'>
   <div id ="health_display" class="stats_right"></div>
</div>

If I try and .html something that isn't a variable, it works. Whenever I .html any variable whatsoever, I get that error.

Anyone know what this is about? Thanks in advance.

2

There are 2 best solutions below

4
On BEST ANSWER

Be sure you're passing a string to html(); if, for example, those are arrays, you can get DOM errors like that.

ie, jQuery("a").html([1,2]) throws the same error; you likely have a type problem.

enter image description here

I wouldn't be surprised if it they're arrays; if you alert([1,2]), it just alerts 1,2, so everything would appear normal.

0
On

I've seen a similar problem with the output being interpreted strangely by .html().

Did you try using .text() and seeing what happened?