3

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.

7
  • If you console.log(data), is there anything there before you get the error? Commented Nov 1, 2011 at 2:46
  • Yes there is, all the data in the array is returned properly. I literally can alert(data['health']); right before the .html line and it outputs fine. Commented Nov 1, 2011 at 2:47
  • 2
    Am I imagining things or is there a missing ) closing brace? Commented Nov 1, 2011 at 2:47
  • I stripped down the code for more ease of reading, it's not a syntax error, I have all the closing tags. I just updated my code for the full function. Commented Nov 1, 2011 at 2:51
  • If you notice, I even tried setting the data['health'] as a variable, alerted that variable, which worked. Then .html'd the variable, and still got the error. Commented Nov 1, 2011 at 2:53

2 Answers 2

4

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.

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

4 Comments

I stripped down the code for more ease of reading, it's not a syntax error, I have all the closing tags.
@Greg Joe is responding to an earlier version of my answer where I mentioned a potential syntax error. I've seen removed it after seeing his edits.
yahelc you were right on with jquery not viewing the item in the array as a string...The value is a number, but it still should be a string...in any case, if I simply set the variable to += ' '; javascript "converts it" to a string, and then it will output correctly...So I now can fix the problem, although I still don't understand why the problem is occurring to begin with.
Note that if you have an array of things you want to append, it should work as expected if you do jQuery("a").html([1,2].join("")).
0

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

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

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.