Roger Bergante
Roger Bergante

Reputation: 55

Javascript nodeValue not accepting new variable

Here's what I have:

I create this markup on the fly:

 <p id="amount"><span></span></p>

Now I need to fill up the span:

if (amount.firstChild.nodeType == 1) {
  amount.firstChild.nodeValue = text;

  alert(description.firstChild.nodeType); //this shows SPAN

}

amount is a variable created with getElementbyId and text is a variable that it works and shows just a figure.

Now, why isn't accepting text as a value? It renders and empty span... and text is working just fine....

thanks a lot fellas!

Upvotes: 0

Views: 183

Answers (3)

cliffs of insanity
cliffs of insanity

Reputation: 3694

You can test for a nested text node in the span, and if there isn't one, create it.

var span = amount.firstChild
if (!span.firstChild)
    span.appendChild(document.createTextNode(text));
else
    span.firstChild.nodeValue = text;

Upvotes: 0

David Thomas
David Thomas

Reputation: 253308

Try using:

amount.firstChild.appendChild(document.createTextNode(text));

JS Fiddle proof-of-concept.

Upvotes: 2

Sully
Sully

Reputation: 14943

amount.firstChild.innerHTML = text;

Upvotes: 1

Related Questions