
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can I get H1 innerText in JavaScript without the innerText of its child?
Getting the inner text of an HTML element is a common task when developing web applications. In JavaScript, it can be done using the innerText property of the HTMLElement object. However, this only returns the text within that particular element and not any of its child elements.
If you need to get just the H1 tag's innertext without including its child elements' innertext, then there are several ways to do so. This article will discuss some methods for getting an H1 tag's innertext in JavaScript without including its child elements' innertext.
The rendered text content of a node, and its descendants is represented by the innerText property of the HTMLElement interface. As a getter, it simulates the text; the user would obtain if they used the cursor to highlight the element's contents and then copied them to the clipboard.
Syntax
Following is the syntax for innertext in JavaScript.
element.innerText
let’s look into the following example to understand more about how can I get h1 innertext in JavaScript without the innertext of its child.
Example
In the following example we are running the script to get innertext in JavaScript.
<!DOCTYPE html> <html> <body> <div class="Bike"> <div class="Car"> <label for="name"><b>Actual Text:</b></label> <h1> This is BMW<span> That Was Ducati </span></h1> </div> </div> <h3>Innertext:</h3> <script> const h1 = document.querySelector('div.Car h1'); const text = h1.childNodes[0].textContent; document.write(text); </script> </body> </html>
Example
Considering the following example where we are running script to get h1 innertext along adding some extra message.
<!DOCTYPE html> <html> <body> <div class="tutorial"> <div class="tutorial1"> <label for="actual"><b>Actual Text:</b></label><br> <h1>Welcome <span>to the Tutorialpoint</span> HELLO </h1> </div> </div> <h1>Innertext:</h1> <script> const h1 = document.querySelector('div.tutorial h1'); const el = h1.childNodes; let result = ""; for(i=0;i<el.length;i++){ if(el[i].nodeName == '#text'){ result+=el[i].textContent; } } document.write(result); </script> </body> </html>
Example
Let’s look into the another example to obtain innertext in JavaScript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <div class="first-header"> <div class="second-header"> <h1> This is h1 tag........<span>This is span tag inside h1 tag.......... </span></h1> </div> </div> </body> <script> var valueOfh1 = document.querySelector('div.second-header h1'); var value = valueOfh1.childNodes[0].textContent; document.write(value); </script> </html>
- Related Articles
- innerHTML vs innerText in JavaScript.
- HTML DOM innerText Property
- How can I remove a child node in HTML using JavaScript?
- How can I remove all child elements of a DOM node in JavaScript?
- How can I update child objects in MongoDB?
- How can I get days since epoch in JavaScript?
- How can I get seconds since epoch in JavaScript?
- How to get the child element of a parent using JavaScript?
- How can I update child objects in MongoDB database?
- How can I get time and date since epoch in JavaScript?
- How to get the product of two integers without using * JavaScript
- How can I get a JavaScript stack trace when I throw an exception?
- How can I get a stack trace for a JavaScript exception?
- How to get the arctangent of the quotient of its arguments in JavaScript?
- How can I get up early in the morning?
