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

Update the table contents smoothly using the html helper PagedListPager without having to reload the entire

I am struggling with a specific line of code that utilizes the html helper's PagedListPager: @Html.PagedListPager(Model.kyc_paged_list, page => Url.Action("ClientDetails", new { id = ViewBag.ID, kyc_page = page, transaction_page = Model. ...

Responsive Material-UI horizontal navigation bar

Hey there! I'm working on developing a UI using Material-UI, and I want to make it so that the menu can be collapsed into a slide-in style menu when accessed on a mobile device. Currently, I'm using tabs which allow for sliding left and right, b ...

Why isn't the parent div stretching alongside its child divs?

Take a look at this example: wthdesign.net/website/olaf&co/index.php I am currently working on creating a responsive layout, but I am struggling to figure out how to make the "#content" div stretch with its child div when there is more content than ex ...

Discovering identical elements within a list in Python through index-based location scanning

Check out this JavaScript code that can determine whether there are any duplicate elements in an array. function findDuplicates(arr) { let seen = []; for (let i = 0; i < arr.length; i++) { if(seen[arr[i]] === 1) { ...

The useLocation state is returning as null

I've encountered a debugging issue with my app. It's a straightforward application that fetches random API images from Spoonacular, allowing users to either select "Yah" or "Nah" similar to Tinder. Upon choosing "Yah", the image should be added t ...

Sending dynamic data from PHP to jQuery flot pie chart

In my PHP code, I have defined 3 variables. $record = "283-161-151"; $rec = explode("-", $record); $win = $rec[0]; $draw = $rec[1]; $loss = $rec[2]; The variables $win, $draw, and $loss are displaying correctly, indicating they are working as intended. ...

What is the method, by using JavaScript or CSS, to include extra space at the end of text block without starting a new line?

Imagine having some text, and at the conclusion of each paragraph there is a (more)... link. The layout ends up looking like this: This is just an example paragraph. Click on the more link for additional information. (more...) Now comes the desire to i ...

Troubleshooting Node.js - MongoDB document removal issue

I am attempting to delete all documents from a collection that contain a field named uuid with values matching the $in operator along with an array I provide. However, for some reason the deletion is not functioning as expected. Below is the code snippet a ...

Is it possible to combine Bootstrap 2.3.1 with newer versions such as 4.0 or 3.x.x?

My current website is built using Bootstrap 2.3.1, but now I am looking to update to a new version of Bootstrap. However, when I tried updating, the entire UI changed drastically and the website started to look clumsy. Instead of completely replacing the ...

Utilizing a directive in contexts beyond a component

I have developed a popover module that consists of two components and three directives. However, I am encountering an issue where I am unable to utilize the directives outside of the main component. Whenever I try to do so, I face an editor error stating: ...

Can you explain the meaning of this PHP syntax and why is this variable showing NaN?

Looking for question marks on Google can be quite challenging. Can someone explain this process step by step? $page = isset($_POST['page'])?$_POST['page']:"0"; I believe it means to check if $_POST['page'] is set, use that ...

What is the best way to align a drop-down menu within my DIV container?

Here is the code snippet I am working with: <div id="one"> <a href="#" id="test1" > <button title="test1">test1</button> </a> <a href="#" id="test1" > <button title="test1">test1</button> &l ...

Prevent selecting dates on <input type="datetime-local"> fields

How can I restrict date selection? I am working on implementing a date picker for clients to choose appointment dates. To prevent clients from selecting dates that are already booked (stored in a database and using Vue), I want to set limitations. Note: I ...

Discovering indistinguishable "Feeling Fortunate" inquiries

My goal is to create a program on my website that can compare the top search results for different queries. For instance, I want it to recognize that the top search result for "twelve" and "12" is the same, leading to https://en.wikipedia.org/wiki/12_(numb ...

What are some key considerations for creating a website that is optimized for printing?

I am working on creating a website filled with content that I want to optimize for printing. My goal is to have the content easily printable without any unnecessary elements like navigation or advertisements. To achieve this, I am considering using two sep ...

Use jquery and json to automatically populate select fields based on specific threshold values when they are changed

Looking for guidance on using jQuery to dynamically populate four select fields based on a four-level JSON array for a webshop, depending on threshold values. I'm seeking help with dynamically changing the selects only if the given value is greater t ...

Transforming Nested JavaScript Objects for Internationalization in Vue

In my VUE JS web application that utilizes i18n-vue, I am facing an issue with reformatting a JS Object to work with the i18n-vue setup. The translations are retrieved from the database and are structured in a certain way as shown below. I have tried seve ...

Issues arise with the Node EJS module due to the malfunction of the include

Struggling with incorporating HTML snippets into my index.html, even after reviewing the EJS documentation. Directory Structure: project -public --assets ---css ---images ---js --Index ---index.html + index.css and index.js --someOtherPageFolder -views - ...

Looking for assistance with troubleshooting CSS issues specifically in Internet Explorer

Hey there! I've been working on a landing page and it's looking good in Safari, Firefox, and Chrome. The only issue is that the layout is all messed up in IE (any version). If any of you experts could take a look at it and give me some suggesti ...

The child's status is not displaying correctly

I am in the process of developing a blackjack app and facing an issue with re-rendering hands after the initial deal. I have tried updating the state in App.js and passing it to PlayerHand.js for rendering, but the child component does not refresh despite ...