Extracting HTML content from a text

var 
    parse = new DOMParser(),
    d = parser.parseFromString('<?xml version="1.0"?><div class="a">Hello</div>', 'application/xhtml+xml');

console.log(d.querySelector('*'));
console.log(d.querySelector('div'));
console.log(d.querySelector('div.a'));

The initial two selectors are functioning properly

Unfortunately, the last selector with the class attribute returns null :(

Does anyone have insight into why this might be happening?

I need to query HTML results from AJAX without adding it to the main DOM.

Answer №1

The reason why your html/xml is not working correctly is because of its invalid structure. Please consider changing </a> to </div>

Answer №2

Put it to the test:

console.log(document.querySelector('div[class=a]'));

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

Using Datatables to Retrieve Data from Server with Ajax via API Endpoint

After making a GET request to an API, I received 1000 accounts. Here is a link to my sample data: Following the recommendations on the Datatable website, here are my settings: var account_table = $('#account-table').DataTable({ "processing" ...

Introduce a timeout in the ajax request

I'm currently facing an issue with adding a 2-second delay between the loader icon and the success message displaying the data as html. I attempted to use setTimeout in my ajax code with a specified delay number, but it doesn't seem to be workin ...

What is preventing me from creating a perfect circle using the arc() method in Three.js?

While attempting to create a complex shape in Three.js using extruded arcs, I encountered some unexpected behavior. I am unsure if my understanding of the API is lacking, but wasn't this code supposed to generate a full extruded circle with a radius o ...

Ways to conceal a particular tab when switching to a different tab

My goal is to have a modal display a date/time set by another party when opened by the user. However, I also want to provide an option for the user to propose a new time in the footer so that they can request a different meeting time from the other party ( ...

Why isn't my onScroll event triggering in my React.js application? What mistake am I making?

I am facing an issue with my onScroll event in react js. My goal is to implement infinite scrolling in react js, but unfortunately, the onScroll event is not triggering as expected. The process involves fetching posts from an API and passing them to the ...

Guide to displaying JSON.stringify data in PHP

I am attempting to retrieve my Google contact list using the Contact API. I have successfully retrieved the result and it is displaying in the console of both Chrome and Firefox browsers. However, I want to print this data using PHP on the same page. < ...

Is there a way to eliminate a control from Ajax mode within a repeater?

Currently, I am utilizing radajaxmanager to enable ajax mode on my website. However, there are instances where I need to exclude a specific control, like a link button within a repeater, from the ajax mode (even though the repeater itself is included in r ...

What is the process of enabling scrolling on the main panel once scrolling on the sidebar has concluded?

How can I achieve a scrolling behavior similar to the one demonstrated here? When scrolling through the sidebar, I want it to continue scrolling until it reaches the end. After that, any further scrolling on the sidebar should scroll the main panel inste ...

Adding HTML content inside an iFrame at a specific cursor position in Internet Explorer

Does anyone know a method to insert HTML into an iFrame specifically for IE? I have been using execCommand insertHtml in Chrome and Firefox, but it doesn't seem to work in IE. I was able to paste HTML into a content editable div using pasteHTML, howe ...

Utilize Node.js driver to export a Mongo collection into a JSON file

How can I export a MongoDB collection to a JSON format using the Node.js driver and fs.writeFile? ...

Receiving a 400 Bad Request message when attempting to send JSON data via AJAX post request

Despite reading numerous questions about this problem on stackoverflow, nothing has helped me so far. Here is the function I am struggling with: function ip_login(){ //alert(JSON.stringify(arr_login)); $.ajax({ crossdomain: true, ...

Quickly Share Documents within a Folder

I currently have a server set up with the following file structure: - index.js /client -index.html -about.html -signup.html -style.css Within the index.js file, I have implemented an express server. When the user navigates to /, it should display the ...

Exploring the ins and outs of troubleshooting Nodewebkit using Webstorm

Attempting to open a NW in Webstorm for debugging purposes, but encountering an issue. When there is an error in the app, the NW window simply closes without providing any indication of what went wrong. Stumbled upon this helpful article on the Webstorm w ...

Creating an enum from an associative array for restructuring conditions

Hey everyone, I have a situation where my current condition is working fine, but now I need to convert it into an enum. Unfortunately, the enum doesn't seem to work with my existing condition as mentioned by the team lead. Currently, my condition loo ...

How to refresh an image in Next.js using setState even when the src path remains unchanged

Define a state variable: const [picture, setPicture] = useState(null); Assuming the picture is initially set to "123" and even after updating the image, the value remains "123". How can I reload the Image? <Image src={profileurl + picture} alt="profile ...

Comparing boolean values in React JS

I am working with a React JavaScript code in which I am iterating through a series of boolean values. The issue I am facing is that when 'data.nextrow' is false, I expect nextrow1 to also be false but it ends up being set to true instead. co ...

Having trouble populating the dropdown menu with data retrieved from the database

I'm having an issue fetching values from the database to populate a drop-down list using jQuery. Unfortunately, nothing is displaying in the dropdown list. Below is the code snippet: getlist.php <?php $conn =mysqli_connect("localhost&q ...

What is the best approach for extracting functions from express routes for unit testing?

I have a JavaScript controller containing some exported routes. I am interested in accessing the functions used within these routes for unit testing purposes. While many blogs suggest creating actual HTTP requests to test express routes, I consider this t ...

How to Implement Snap-Enabled Dragging in a Container Using jQuery UI

Issue Description (Fiddle): I am using the jQuery UI's .draggable() function to make the red element snap inside the blue container. However, it is snapping to one edge only instead of completely fitting inside (both edges). It requires further dragg ...

Automatically refreshing a list upon form submission in React.js

I recently discovered a method to fetch and post data to a database, which is then displayed in a list when the page loads. However, I am facing an issue where the list does not update automatically when a new item is submitted through the form. Currently, ...