Skip to content

Instantly share code, notes, and snippets.

@7cc
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save 7cc/c5ebc9e8c238510a0c42 to your computer and use it in GitHub Desktop.

Select an option

Save 7cc/c5ebc9e8c238510a0c42 to your computer and use it in GitHub Desktop.
data nodeValue textContent [js],[dom]

to insert/change a textNode value, there are 3 ways.

text.data = null
text.nodeValue = undefined
text.textContent = undefined

Whichever method you use, be sure to take a fallback for undefined/null.

text.data = str || ""

Otherwise, an unexpected string will be inserted.

Compat

param/method FF30 IE11 Opera Chrome31 iPad他モバイル
undefined/textContent "" undefined undefined undefined undefined
undefined/nodeValue "" undefined undefined undefined undefined
undefined/data undefined undefined undefined undefined undefined
null/textContent "" "" "" "" ""
null/nodeValue "" "" null "" ""
null/data "" null "" "" ""
var fns = [
  "textContent",
  "nodeValue",
  "data"
]

var args = [
  undefined,
  null
]

args.map(function(e){
  return fns.map(function(ee){
    var text = document.createTextNode("")
    text[ee] = e
    return e +"/"+ ee + "|" + (text.data||'""')
  }).join("\n")
}).join("\n")

textContent is different

textContent breaks all children of node(element).

<div>
  <p>text</p>
</div>
div.textContent = "" // breaks p element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment