Table of Contents
Overview
Algorithm
Example
结论
Home Web Front-end HTML Tutorial How to count the number of lines of text inside a DOM element

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

Aug 20, 2023 pm 05:49 PM
Count rows Number 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;
Copy after login

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;
Copy after login

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>
Copy after login

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;
Copy after login

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;
Copy after login

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

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

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>
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles