Extracting responseXML data specifically for CSS styling purposes

I am working on a simple XMLHttpRequest setup that fetches data from an xml file and displays it in a div on my webpage within a ul element:

var requestData;
if (window.XMLHttpRequest){
    requestData = new XMLHttpRequest();
}else{
    requestData = new ActiveXObject("Microsoft.XMLHTTP");
}
requestData.open("GET", "data.xml");
requestData.onreadystatechange = function(){
    if((requestData.readyState ===4) && (requestData.status ===200)){
        var itemList = requestData.responseXML.getElementsByTagName('title');
        var outputData = "<ul>";
        for (var i=0; i<itemList.length; i++){
            outputData += "<li>" + itemList[i].firstChild.nodeValue + "</li>";
        }
    outputData += "</ul>"
    document.getElementById('update').innerHTML = outputData;
    }
}
requestData.send();

The data is being displayed correctly on the page, but when I check the source code, the new list structure is not visible in the html. Additionally, when I try to style the newly fetched list using CSS like below:

$('document').ready(function(){
    var cssStyle = {
        "background-color": "#333",
        "color": "#FFF",
    }
    $("ul:last li").css(cssStyle);
});

It doesn't seem to have any effect. After importing the data into my html, how can I target it with CSS and apply styling?

Answer №1

Content is loaded using ajax, so it will not appear in the view source. It's recommended to apply styles after making the request, rather than on document ready. Alternatively, you can use classes instead of inline styles, allowing the styles to automatically update when loading HTML onto the page.

You can view new content in Chrome or Firefox by inspecting the element.

request.onreadystatechange = function () {
    if ((request.readyState === 4) && (request.status === 200)) {
        console.log(request);
        var items = request.responseXML.getElementsByTagName('title');
        output = "<ul>";
        for (i = 0; i < items.length; i++) {
            output += "<li>" + items[i].firstChild.nodeValue + "</li>";
        }
        output += "</ul>"
        document.getElementById('update').innerHTML = output;
        var cssObj = {
            "background-color": "#333",
                "color": "#FFF",
        }
        $("ul:last li").css(cssObj);
    }
}

View demo

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

Uncovering the origin of surprising reactivity issues in Vue 3 component workings

In my current project, I'm working on a basic Vue-3 app and encountering an issue that's puzzling me. The view in question is rendered through the router and consists of simple state components - an object reference for storing text field values ...

How can I combine an onkeypress and onclick event listener in one method?

I have two inquiries, somewhat intertwined. 1) Is it feasible to merge the 2 functions below into a more efficient function, or do I need to create a function and then call it in both event listeners? input.addEventListener("keyup", () => { if (eve ...

Tips for emphasizing a specific cell in a row based on its expiration date

In my quest to find a script that colors dates within two weeks from today in red and past dates in orange, I have tried various methods without success. I am struggling to implement this feature with my current knowledge. <TABLE> <TR><TD&g ...

Struggling to retrieve an Object from an HTMLSelectElement within an Angular project

Can you help me with this code snippet: <select #categorySelect (change)="goToCategory.emit(categorySelect.value)" style="..."> <option selected hidden>All</option> <option [ngValue]="categ ...

"Exploring the MUI Data Grid: A Step-by-Step Guide to Adding a Summary Row

Need help with adding a summary row to my Data Table from MUI. You can find the documentation here or visit the main website here. I have tried to implement the summary row but I am facing some challenges. Any suggestions or ideas would be highly apprecia ...

The jqxgrid from jqwidget is displaying all content in a single, messy column

After successfully publishing my project from ASP.NET MVC, everything was functioning perfectly. However, upon publication, the JQXWidget JqxGrid displayed data in a mixed-up manner as shown in this image: https://i.sstatic.net/0o1XY.jpg The layout of my ...

React Navigation Item Toolbar Misplacement

I've been trying to align the navigation item with the logo on the same line within the toolbar, but I'm facing an issue where they keep appearing in different rows. To see the error for yourself, check out the code sandbox here. This is how I s ...

What is the best way to create a personalized email form using javascript and reactjs without it opening in a new window?

I'm in the process of creating a contact section for my portfolio website that mimics an email interface. Instead of just providing a link, I want to include a complete form so visitors can easily reach out without needing to open a new window or exte ...

The deletion function necessitates a switch from a server component to a client component

I built an app using Next.js v13.4. Within the app, there is a server component where I fetch all users from the database and display them in individual cards. My goal is to add a delete button to each card, but whenever I try to attach an event listener t ...

The z-index property in jQuery dialog does not seem to function properly when used within an

When using bootstrap with jQuery dialog, I encountered a strange issue. If the z-index of the dialog is set to 1 or higher in a standalone script, everything works fine. However, when the same script is called inside an iframe, the dialog ends up appearing ...

Shift a div out of view (using margin: 0px auto)

How can I achieve a sliding effect for two horizontally centered divs, #box1 and #box2, when a button with the class .class is clicked? The desired animation involves moving #box1 to the left and off the screen, while #box2 slides into view from the right. ...

Greasemonkey: Enhance HTML Files by Highlighting Multiple Words

Is there a way to use Greasemonkey to highlight specific words in text? For example, if I want to highlight "Basel" or "Bern", it works for one word but not both. The code below kind of does the job, but when trying to highlight two words it doesn't w ...

Use radio buttons to enable switching between different data options within a dropdown menu

I am working on a dropdown box that is populated dynamically using JSON data. The content in the dropdown can be categorized into 4 types, so I have included radio buttons to switch between these categories. I have created HTML code to toggle between manu ...

Using ChartsJs to visualize input data formatted in the German data format

I am relatively new to working with Charts.js, but I will need it to generate some visually appealing graphs for my website. In the background, I have a Django project running that calculates a specific set of numbers for me. Due to the language setting in ...

Struggling to pinpoint the exact element in Python/Selenium

As I work on creating a website manipulation script to automate the process of email mailbox creation on our hosted provider, I find myself navigating new territory in Python and web scripting. If something seems off or subpar in my script, it's beca ...

The node server.js encountered an error - Module not found

After attempting to start my node server using the following command: node server.js An error was thrown: internal/modules/cjs/loader.js:905 throw err; ^ Error: Cannot find module 'fcc-express-bground' Does anyone have any solutions? ...

Retrieve specific information by clicking a button within a table cell on the data grid in React.js

I am just starting out with React and have a query. I have set up a data table with some dummy information using a React data table. How can I make the cells of the table clickable so that when clicked, the data is displayed on a form located on the same p ...

Creating one div to initiate the contents to fadeToggle and another div to cease the fadeToggle animation utilizing the setInterval function in JavaScript

Here is the link to my Fiddle: http://jsfiddle.net/tmanundercover/62ap2/ CSS #targetedDiv, #otherDiv { border: 1px solid; } HTML <div id="targetedDiv">&nbsp<span id="fakeCursor">|</span></div> <br> <div id="othe ...

Guidelines for implementing drag and drop functionality for my specific scenario

Looking to create a restricted area for dragging elements within my app. Specifically, I want the element to only be draggable within a certain defined space. Here is what I currently have: http://jsfiddle.net/L73T5/10/ html <div id='background ...

Implementing postcompose functionality from OpenLayers 3 into an OpenLayers 6 event within a Vue Single Page

Check out this codepen I created where I modify the basemap context of an OpenLayers map on the postcompose event. Now, I'm facing a challenge in translating this working demo to a Vue SPA using OpenLayers 6. I want to link the dark mode map effect to ...