search
HomeWeb Front-endHTML TutorialHow to count the number of lines of text inside a DOM element
How to count the number of lines of text inside a DOM elementAug 20, 2023 pm 05:49 PM
Count rowsNumber of lines of dom text

How to count the number of lines of text inside a DOM element

Overview

The number of the text lines inside the Document Object Model (DOM) element whether it would be any element that is

,

or

In the second method, we will use the split() method of the array, where we will pass the newline character as a parameter. So the array will be created from the elements before and after the newline character and we will calculate the length of the array.

Algorithm

Step 1 − Create a HTML template, add a div element in it with line-height. Line-height is the height of the particular line. Also create a span tag in which the output will be generated.

Step 2 Now access the parent element of the row in the script tag. Use offsetHeight to calculate the total height of the parent element.

var domHeight = a.offsetHeight;

Step 3 Use the style attribute to get the row height value of the row. Since the row height value also contains the unit "px", we need to use the parseInt() function to get the number from it.

Step 4 Now split the extracted values, namely domHeight and line-height.

var totalLines = domHeight / linesHeight;

Step 5 - The variable "totalLines" contains the number of lines in the parent element. This will return the number of rows in the span tag.

The Chinese translation of

Example

is:

Example

In this example, we will count the number of rows by calculating the height of the entire Document Object Model (DOM) element, using the offsetHeight property to get the height of the DOM element, and dividing the DOM height by the row High, this will give us the output of the total number of rows present in the DOM.

<html>
<head>
   <title>Count the text lines inside of DOM element</title>
</head>
<body>
   <div id="lines" style="line-height: 30px;">
      Welcome to tutorialspoint<br>
      Visit us at: https://www.tutorialspoint.com/<br>
      Buy the course of your domain <br>
      Get the certificate <br>
      Thank you for visiting<br>
   </div>
   <strong style="border: 2px solid black; padding: 0.1rem;">
      Total number of lines:
      <span id="out"></span>
   </strong>
   <script>
      var a = document.getElementById("lines");
      var domHeight = a.offsetHeight;
      var linesHeight = parseInt(a.style.lineHeight);
      var totalLines = domHeight / linesHeight;
      document.getElementById("out").innerText=totalLines;
   </script>
</body>
</html>

In the image below, it shows the output of the above example. It contains the "30px" line height, which splits the "offsetHeight" of the DOM element, and returns the number of elements in the span tag.

Algorithm

Step 1 Create an HTML template and add a div element to it. Create a span tag used to generate output.

Step 2 Now access the text inside the parent element using the document.getElementById().

const textLines = document.getElementById("lines").innerText;

Step 3 Use the arraysplit() method to separate the newline character (“

") is passed to it as a parameter. The split method stores the line of text as one element in an array.

Step 4 Now we will use the length method of array to calculate the length of an array which will return the number of text lines in the element.

const numLines = textLines.split("").length;

Step 5 Display the output of the number of text lines in span tag using

document.getElementById("out").innerText=numLines-1;

Here we subtract one from "numLines" because it contains an extra line break element that is inserted before the start of all lines of text.

The Chinese translation of

Example

is:

Example

In this example, we will calculate the number of lines by using the array split() method in which we will access the DOM in a variable and with the help of split(“

” ) method we will pass a line break as argument which will store all the element in an array as the line breaks. After which we will calculate the length of an array, which will return the number of text lines available in the DOM .



   Count the text lines inside of DOM element


   
Welcome to tutorialspoint
Visit us at: https://www.tutorialspoint.com/
Thank you for visiting
Total number of lines: <script> const textLines = document.getElementById(&quot;lines&quot;).innerText; const numLines = textLines.split(&quot;&quot;).length; document.getElementById(&quot;out&quot;).innerText=numLines-1; </script>

In the below image, it shows the output of the above example. In which all the three text lines are stored in the array as an element. So on calculating the length of an array it will return 3.

结论

In the first example we had to parse the value of the line-height with parseInt() because it contains the string value also and if we divide that value from the “offsetHeight” value then it will return (NaN) which means “Not A Number”. So we had used parseInt(), but we can also use parseFloat() if we want to return it in decimal.

The above is the detailed content of How to count the number of lines of text inside a DOM element. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft