Obtain the text within HTML tags using JavaScript

I've received an array of strings in this particular format.

<data><summary>SUMMARY</summary><details>DETAILS</details></data>

Is there a way to extract the text within the <summary> </summary> tags using JavaScript?

Answer №1

Give this a shot:

let text = "<information><heading>HEADER</heading><content>CONTENT</content></information>";
if (window.DOMParser) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text, "text/xml");
} else // For older versions of Internet Explorer
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(text);
}

console.log(xmlDoc.getElementsByTagName('information')[0].childNodes[0].childNodes[0].nodeValue);

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

What is the process for generating an array of objects using two separate arrays?

Is there a way to efficiently merge two arrays of varying lengths, with the number of items in each array being dynamically determined? I want to combine these arrays to create finalArray as the output. How can this be achieved? My goal is to append each ...

How can you selectively conceal the nth item in a v-for loop while keeping the original array intact? Discover a way to do this using the search function

I have a reference variable called foxArticles that holds a list containing 100 items. Using a v-for loop, I iterate over each value and render all 100 values on the page. <template> <div class="news_container"> <div v- ...

Discover the process of connecting a REST controller with AngularJS and Spring

I have a list of file paths stored in an array within the scope variable of an AngularJS Controller. My goal is to properly map these file paths to a Java REST controller for further processing. When I pass it as "/ABC/Scope-Variable," it successfully ma ...

React Js month picker problem encountered

I am currently working on implementing a month picker using the library called react-month-picker The code is functioning correctly in React: https://codesandbox.io/s/react-month-picker-forked-84rqr However, when I tried to integrate the same code into a ...

What are the steps to utilize Angular-fullstack without any server components?

The way Angular-Fullstack scaffolds the project is really impressive. However, I already have data being served through a restful API, so server components are unnecessary for me. Is there a way I can utilize only the client part and eliminate the server c ...

Using Composition API to call a method on a child component

In my current project, I am facing a challenge with a parent component needing to call a method from one of its child components: <template> <div> <button @click="callChildMethod"> <child-component ref="child&q ...

Vue.js Applications tend to encounter Expected Errors originating from JavaScript

I've been diving into learning Vue.js using Visual Studio 2017. Currently, my goal is to create applications with multiple buttons that trigger the display of a message upon being clicked. However, when I attempt to run this code, I encounter the foll ...

The ng-bootstrap datepicker does not allow setting a default date prior to selection

I have implemented the ng-bootstrap date picker in my project and I am facing an issue with setting a default date for the input field before selecting a date from the datepicker itself. <input type="text" id="datepicker{{i}}" class="form-control" form ...

Prevent duplicate form submissions when reloading using AJAX and Solve the header modification restriction issue

I've encountered an issue where the form is being resubmitted upon page refresh. While there are solutions available online for this problem, my situation is unique because I'm utilizing ajax to submit the form, resulting in only a section of the ...

The specified container is not a valid DOM element. (Utilizing Ant Design carousel component in React.js)

Here is a snippet of my code where I am having an issue with my carousel. It seems to be giving me an error message "Target container is not a DOM element" when I try to run the server. I have divided my code into two parts - the first part containing the ...

Warning: An unhandled promise rejection has occurred due to a TypeError, as the property 'title' cannot be read since it is undefined

I am encountering an error related to the router.post("/") function in my JavaScript, express, ejs, and MongoDB project. The main objective of this post request is to enable users to save a blog post, which will then redirect them to the main page ("/"). ...

Is it possible to send a data attribute from an HTML element using AJAX to a some.php page when it is clicked?

One scenario involves retrieving the product id from a database and inserting it into a data-idproduct attribute. <a class="cart" href="" data-idproduct="<?php echo $itemArtikal['id_product'] ?>">Add to C ...

The event fails to propagate up to the parent component

I have a project structure set up as follows: https://i.stack.imgur.com/bvmK5.jpg The todo-form component triggers the created event and I am looking to handle this event in the todos component. todo-form.component.html: <form class="todo-form" ( ...

Is there a way to attach event listeners to raw HTML that has been imported using code?

I'm facing a challenge with importing SVG code embedded in an HTML file and using the v-html directive to render it. The issue is that I need to attach click events to the anchor tags within that imported file, which are not part of my main template. ...

Finding the coordinates of a DOM object in a document using jQuery

Does anyone know how to retrieve the X and Y coordinates (in relation to the document's top/left) for a specific DOM element? Despite my search, I haven't been able to find any plugins or jQuery properties that can provide this information. While ...

ChartJs has encountered the issue of reaching the maximum stack call size, leading

I'm currently facing a challenge while trying to create a line graph using ChartJs to plot numerous points. The graph works perfectly fine when I have 600000000 values or less (6E8), but it fails to display anything beyond that threshold. My goal is t ...

Is it more effective to utilize a filter or request fresh data when filling in a form's drop-down choices?

Utilizing an axios ajax request, I am retrieving a JSON list of tags related to a specific topic selected from a dropdown. If no topic is chosen, all tags in the database (approximately 100-200 tags) are fetched. The process involves: User selects a topi ...

Utilizing the keyword 'this' within a function of a JavaScript class that is invoked with the .map method

I am working with the RegisterUser class which contains various methods and properties: class RegisterUser { constructor(username, password, ispublic){ this.username = username; this.password = password; this.ispublic = ispublic; this.id ...

Is the Date Epoch a reliable form of unique identification?

Currently, I'm developing a Node API and encountered a situation where I need to create a unique 15-digit random number for a model without using autoincrement. The challenge is to ensure that the generated number is not trivial. I am hesitant about ...

Tab buttons that switch between different sections with just a click

Forgive me if this seems like a simple coding issue, but I struggle with javascript and need some guidance... I have a setup where two buttons (blue and yellow) toggle between different content divs. On another part of the page, there are also two buttons ...