Extract the raw text content from nested elements

Working with highlight.js to include a custom CSS code, however, this library automatically adds span tags around the desired text

For example:

<pre>
<code class="language-css hljs" contenteditable="true" id="css-code">
<span class="token selector"&bt;<span class="hljs-selector-class"&bt;.spacer-line</span&bt;</span&bt; <span class="token punctuation"&bt;{</span&bt;
    <span class="token property"&bt;<span class="hljs-attribute"&bt;border-bottom-width</span&bt;</span&bt;<span class="token punctuation"&bt;:</span&bt; <span class="hljs-number"&bt;20px</span&bt;<span class="token punctuation"&bt;;</span&bt;
    <span class="token property"&bt;<span class="hljs-attribute"&bt;width</span&bt;</span&bt;<span class="token punctuation"&bt;:</span&bt; <span class="hljs-number"&bt;50%</span&bt;<span class="token punctuation"&bt;;</span&bt;
    <span class="token property"&bt;<span class="hljs-attribute"&bt;border-color</span&bt;</span&bt;<span class="token punctuation"&bt;:</span&bt; black<span class="token punctuation"&bt;;</span&bt;;
<span class="token punctuation"&bt;}</span&bt;
</code&bt;
</pre&bt;

The actual plain text is:

.spacer-line { border-bottom-width: 20px; width: 50%; border-color: black; }

I require the plain text. How can I achieve this?

UPDATE

If I use the following code for any reason:

let value = document.querySelector('pre');
console.log(value.textContent);

I am able to retrieve the pre and code tags without the span tags The resulting value is:

<pre class=" language-css"&bt;<code class="language-css hljs"&contenteditable=true&id="css-code"&bt;></code&bt;
</pre>

and an empty value for textContent

Answer №1

To retrieve the text content of a selected element, you can use either innerText or textContent:

console.log(document.querySelector('pre').textContent.trim())
<pre>
<code class="language-css hljs" contenteditable="true" id="css-code">
<span class="token selector"><span class="hljs-selector-class">.spacer-line</span></span> <span class="token punctuation">{</span>
    <span class="token property"><span class="hljs-attribute">border-bottom-width</span></span><span class="token punctuation">:</span> <span class="hljs-number">20px</span><span class="token punctuation">;</span>
    <span class="token property"><span class="hljs-attribute">width</span></span><span class="token punctuation">:</span> <span class="hljs-number">50%</span><span class="token punctuation">;</span>
    <span class="token property"><span class="hljs-attribute">border-color</span></span><span class="token punctuation">:</span> black<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code>
</pre>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with displaying search form div using jQuery AJAX

Currently, I am in the process of developing a search form that includes a dropdown menu. The functionality involves selecting an item from the dropdown menu triggering an AJAX call to another PHP file. This call returns tags which resize both the dropdown ...

React unit tests experiencing issues with MSW integration

After creating a basic React application, I decided to configure MSW based on the instructions provided in order to set it up for unit tests in both node environment and browser. The main component of the app utilizes a custom hook called useFormSubmission ...

Achieving full width for the elements in a row with Flexbox: A step-by-step guide

I am dealing with a flexbox grid that contains random width images: https://i.stack.imgur.com/pfPeU.jpg I want to stretch the elements in each row to full width to eliminate any white space - Is there a way to achieve this using CSS? justify-content do ...

Retrieve information from a JSON file within a Vue.js application rather than entering data manually

I am venturing into the world of Vue.js for the first time. I have created an app that currently relies on manually added data within the script. Now, I am looking to enhance it by fetching data from a JSON file, but I'm unsure about how to proceed wi ...

Error: Attempting to access the properties `line_items.amount`, `line_items.currency`, `line_items.name`, `line_items.description`, or `line_items` is not allowed

Hi there, I'm currently working on creating an Amazon-inspired platform and I've encountered an error while trying to integrate Stripe with the project. Can anyone provide some assistance? You can refer to the video tutorial I'm using by fol ...

Encountering the "TypeError: document.getElementById(...) is null" error message while utilizing react.js in conjunction with chart.js

I am encountering an issue while using react and chart.js together to create a bar chart. The problem lies in the fact that chart.js requires the use of canvas tags, and we need to locate this tag and insert the bar chart within it using the traditional do ...

Does an href and click events both happen simultaneously?

JavaScript Purpose: Implement a series of loops and create anchor links with the 'href' attribute <a class="mui-control-item" v-for="(item, index) in dai" v-on:click ="abc(item)" :href="'#item'+(index+1)+ 'mobile'" ...

Tips for enabling unrestricted unencoded HTML in XmlNode or XmlElement with XmlSerializer

Looking for a solution to generate an XML element that can handle a string of text containing HTML or other valid XML elements. Here's an example: "Test text with <strong>custom nodes</strong> that shouldn't be encoded" I've at ...

Utilize Postman to send a JSON body in a POST request to trigger a stored procedure on Microsoft SQL Server

I need to send a JSON request using the Postman app, utilizing the POST method to retrieve data. My boss, who is overseeing my training, assigned me this task. I've scoured the web for a week but haven't found a solution yet. Initially, I sugges ...

How can I use jQuery to access the parent node in an XML document?

I have been trying to extract the top-level 'label' attribute from the XML code below using jQuery, but I haven't had any luck so far. I have already converted it into a DOM object, but the results are not what I expected. Does anyone have a ...

Tips for avoiding overflow while utilizing animations

In my current project, I am facing an issue with a container div that has the CSS property overflow: auto which cannot be removed. Inside this container, there is another div that toggles between showing and hiding using an animation triggered by a button ...

The jQuery Deferred feature on Ajax is failing to properly pass the array in the data option as an array, instead converting

I am facing an issue in my application where I want to use jQuery deferred to handle Ajax success and error uniformly from a central location. It works perfectly fine when I pass strings in the data option, but when I try to pass an array, it gets sent as ...

Integrate user interface functionality within the server component in NextJS

When considering the Server Component located in the new app/ directory within NextJS: export default async function RootLayout({ children }) { const categories = await getCategories(); // const [navigation, setNavigation] = React.useState('hidde ...

Shades of Grey in Visual Studio Code

Whenever I use VSC, I notice these odd grey boxes that appear in my editor. They seem to be dynamic and really bother me. Interestingly, switching to a light theme makes them disappear. However, I prefer using a dark theme and haven't been able to fin ...

The UI does not display the html code (specifically svg) added through jquery appending

I am working with the following HTML code: <svg> <g> <rect></rect> <text> abc </text> </g> <g> <rect></rect> <text> def </text> </g& ...

Troubleshooting: Issues with $oclazyload in AngularJS 1.5

I am currently encountering an issue with the implementation of $oclazyload to defer loading of my components. The code snippet below illustrates the setup: import angular from 'angular'; import uiRouter from 'angular-ui-router'; impor ...

Tips for choosing nested elements with basic CSS selectors like nth-of-type or nth-child for Selenium

Is there a way to select the paragraph tag for B (or C) by utilizing nth-child or nth-of-type in Selenium WebDriver? <tr> <td> <p class="myClass">A</p> </td> </tr> <tr> <td> <p ...

Having trouble running Protractor with the Angular Seed Basic app in AngularJS?

After cloning the angular-seed to my basic setup, I attempted to run protactor using the command below and encountered an error: npm run protractor npm ERR! node v5.0.0 npm ERR! npm v2.10.1 npm ERR! code ELIFECYCLE npm ERR! [email protected] protr ...

How can you retrieve input radio button values using javascript or ajax and display or hide a button accordingly?

I've created a form that includes radio input buttons and a textarea field as follows: <input type="radio" value="2" id="order_status"> Review <input type="radio" value="1" id="order_status"> Accept <input type="radio" value="2" id="or ...

Having Trouble Showing Loading Component on Next.js v13

Having some issues with setting up a loading component in my Next.js v13 project. I followed the documentation by creating a file called loading.tsx in the src directory, but it's not appearing on the page as expected. I've also included a functi ...