Null is not null? Sure it is – a JavaScript tale

Under the category of “huh?” (which I should definitely add to the Categories list in WordPress), I give you this. I was trying to update a SPAN tag with JavaScript code. Simple enough. Here is the JS code I was using:

   var Message = document.getElementById('txtTest');
   Message.innerText = 'With my last breath, I spit at thee';

(I’m pretty sure that quote is in one of those “Glad we’re getting a divorce” cards from Hallmark.)

 The element I was trying to update was defined as so:

   <span id="txtTest" />

 And when I ran the script, I got this error:

      Error: 'null' is null or not an object 

Well, yes, null is in fact null, so what’s the problem?

 Anyways, I was able to solve the issue by creating a closing tag for my SPAN:

      <span id="txtTest"></span>

Feel free to enlighten me on this, but to me, it seems odd.

  1. #1 by Adam on July 26, 2011 - 7:46 pm

    The end tag is required for a span tag:

    When omitted, there was no innerText property to manipulate (by definition), so you tried to manipulate a null value:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: