Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

innerText vs textContent

// textContent gets the content of all elements, including <script> and <style> elements.
// textContent returns every element in the node.
// innerText only shows “human-readable” elements.
// innerText looks at styling and won't return the text of “hidden” elements.

<style>
.special { display: none; }
</style>
<h1>Heading <span class="special">Special</span> </h1>

const h1 = document.querySelector('h1');
console.debug(h1.textContent); // " Heading Special "
console.debug(h1.innerText); // "Heading"
Comment

textcontent vs innertext in JS

You can forgot all the points that may confuse you but remember 3 things:

When you are trying to alter the text, textContent is usually the property you are looking for.
When you are trying to grab text from some element, innerText approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied to the clipboard. And textContent gives you everything, visible or hidden, including <script> and <style> elements.

In element nodes, innerText evaluates <br> elements, while textContent evaluates control characters:
Comment

PREVIOUS NEXT
Code Example
Javascript :: push input value to array javascript 
Javascript :: electron get exe path 
Javascript :: javascript Multiline Arrow Functions 
Javascript :: how to check if item is in list js 
Javascript :: if jsp 
Javascript :: iterata a array in js 
Javascript :: check if message mentions users discord js 
Javascript :: settimeout inside loop 
Javascript :: How To Set Opacity of a View In React Native 
Javascript :: get message author discord.js 
Javascript :: click button javascript 
Javascript :: select document jquery 
Javascript :: dart list files in directory 
Javascript :: installing prop-types 
Javascript :: vue dynamic component props 
Javascript :: click outside javascript 
Javascript :: tabuada js 
Javascript :: REACt helmet og tags DONT WORK 
Javascript :: javascript get first property of object 
Javascript :: binary to int javascript 
Javascript :: jquery see if checkbox is checked 
Javascript :: javascript password regular expression 
Javascript :: how to submit form using ajax 
Javascript :: body click function removeclass 
Javascript :: how to add text to h2 with jquery 
Javascript :: generate thumbnail of pdf using pf js 
Javascript :: prevstate in usestate 
Javascript :: how to remove a property from an object in javascript 
Javascript :: swal con input 
Javascript :: mongoose id from string 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =