Home>

**

**

Is it possible to get the element without firstChild.

Please describe your question in detail.

We are considering rewriting the contents using nodeValue in Javascipt,
It won't be rewritten without firstChild.
Do you have any description without firstChild.

Error message It will not be rewritten unless

firstChild. is attached.

### Corresponding source code
<! DOCTYPE html>
<html>
<head>
    <title>test</title>
    <meta charset = "UTF-8">
</head>
    <body>
      <p>t<strong>es</strong>t</p>
      <script tyoe = "text/javascript">
      let nodelist = document.getElementsByTagName ("strong");
          nodelist.item (0) .nodeValue = "Strong"
          //nodelist.item(0).firstChild.nodeValue = "Strong" // Rewrite
      </script>
    </body>
</html>

Enter the language name here
javascript

Supplemental information (FW/tool version etc.)

chrome latest
Please include more detailed information here.

  • Answer # 1

    Hello

      

    Description without firstChild.

    If

    is a requirement for your question, how about using textContent instead of nodeValue?

    nodelist.item (0) .textContent = "Strong";

    Operation check sample: https://codepen.io/jun68ykt/pen/VwwNNVd

    Reference: MDN-textContent

  • Answer # 2

    Simple,

    nodelist [0] .textContent = "Strong";

    ThenodeValuefor the node itself isnull.
    If no text node is specified,nodeValuewill not be text.
    nodelist [0] .firstChildis a text node.