2

I apply ".length" on the element has id="name", but it counts 29 instead of 14. I wonder where is my mistake? It would be nice if someone can let me know. Thank you!

var name=document.getElementById('name');
var totalTiles=document.getElementById('totalTiles');

totalTiles.textContent=name.length;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
    <div id="heading"></div>
    <table>
        <tr>
            <th>Custom sign:</th>
            <td id='name'>Montague House
            </td>
        </tr>
        <tr>
            <th>Total tiles:</th>
            <td id='totalTiles'></td>
        </tr>
    </table>
    <button>Pay Now</button>
    <script src="script.js"></script>
</body>
</html>

6
  • 1
    console.log the name element itself instead of it's length and you will see why Commented Sep 22, 2019 at 12:52
  • name.textContent.length Commented Sep 22, 2019 at 12:53
  • Spaces count... Commented Sep 22, 2019 at 12:53
  • Where is the is event ,click input or more ? Commented Sep 22, 2019 at 12:57
  • You want the textContent.length of ’name’. Commented Sep 22, 2019 at 13:00

3 Answers 3

5

This is happening because the name variable is the entire dom element (td).

Try using innerText to get the required value

var name = document.getElementById('name').innerText;
var totalTiles = document.getElementById('totalTiles');

totalTiles.textContent = name.length;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Example</title>
</head>

<body>
  <div id="heading"></div>
  <table>
    <tr>
      <th>Custom sign:</th>
      <td id='name'>Montague House
      </td>
    </tr>
    <tr>
      <th>Total tiles:</th>
      <td id='totalTiles'></td>
    </tr>
  </table>
  <button>Pay Now</button>
  <script src="script.js"></script>
</body>

</html>

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

Comments

2

Because the name variable returns [object HTMLTableCellElement] and not the string you intended to return. The length of the [object HTMLTableCellElement] is 29 so that's what the length property returns.

To get the value of the actual string you need to count the length of the variable after applying the .innerText property.

1 Comment

I got it. Thank you!
1

This is because you are returning the entire HTMLTableCellElement on the getElementById selector.

I would recommend you to use textContent or innerHTML to get the text values. However, you should be using textContent instead, as it utilises the text vaules and does not need to parse HTML, thus it is faster.

In addition, you might want to use the trim() method to remove the trailing white spaces.

const name = document.getElementById('name').textContent.trim();
const totalTiles = document.getElementById('totalTiles');

totalTiles.textContent = name.length;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Example</title>
</head>

<body>
  <div id="heading"></div>
  <table>
    <tr>
      <th>Custom sign:</th>
      <td id='name'>Montague House
      </td>
    </tr>
    <tr>
      <th>Total tiles:</th>
      <td id='totalTiles'></td>
    </tr>
  </table>
  <button>Pay Now</button>
  <script src="script.js"></script>
</body>

</html>

1 Comment

Thank you for you help!

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.