At long last, I successfully implemented collapsible styling for XML-generated data, yet I am struggling to get the data

After much experimentation, I have successfully applied JQuery Mobile collapsible styling to data retrieved from an XML file using this code/method. However, I am facing an issue with getting all JQuery styling to work properly - particularly the icon does not display as expected. I want to set the data-collapsed-icon attribute to arrow-u, for example. This seems to work fine in static code but not when working dynamically with an XML/JSON file. Any suggestions on how to make the icon styling function correctly?

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "jargon.xml",
dataType: "xml",
success: function(xml) {

var container = document.getElementById("catalogue");
container.setAttribute('data-role', 'collapsible-set');
container.setAttribute('data-collapsed', 'true');
$(xml).find('release').each(function(){
var release = document.createElement("div");
release.setAttribute('data-role', 'collapsible');
release.setAttribute('data-collapsed', 'true');
var cat = $(this).find('cat').text();
var title = $(this).find('title:first').text();
var artist = $(this).find('artist').text();
var tracks = "";
$(this).find('track').each(function(){
tracks = tracks + $(this).find('title').text() + "<br>";
});
release.innerHTML = "<h3>" + cat + "<br></h3><p>" + artist + "</p>";
container.appendChild(release);

});

var catDiv = $('#catalogue');
catDiv.find('div[data-role=collapsible]').collapsible({theme:'b',refresh:true});
}
});
});

</script>

Answer №1

Apologies for not thoroughly examining your code to locate the specific element, but it appears that the issue may be resolved by the following:

You should invoke jQM's trigger("create") method on the parent container that holds the elements with the jQM data attributes.

Below is a snippet of how the code could be implemented:

$("#myElement").trigger("create");

This action will apply jQuery Mobile functionality and enhance styling for all descendant elements within the chosen element.

Answer №2

Ensure to update the `collapsedIcon` and `expandedIcon` options before adding the collapsible element.

release.collapsible({
    collapsedIcon:"arrow-d",
      expandedIcon:"arrow-u",
        theme:"b"
    }).appendTo("target_div);

Check out the 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

An issue occurred while attempting to utilize fs.readFileSync

Attempting to change an uploaded image to base 64 format var file = e.target.files[0]; var imageFile = fs.readFileSync(file); var encoded = new Buffer(imageFile).toString('base64'); An error is encountered: TypeError: __W ...

Retrieving information from a JSON file and dynamically displaying it on an HTML page using JavaScript

I've been working on pulling data from a JSON file to display on my website. Following this helpful guide at: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON, but unfortunately, I'm facing an issue where nothing is showing ...

Tips for updating property values when calling a TypeScript function

Hello everyone, I am looking to convert a snippet of JavaScript code into TypeScript. JavaScript function newState(name){ var state ={ name : name, age : 0 } return state } function initStates() { this.JamesStat ...

The JavaScript function String.split() is generating an array filled with 20 empty strings

Attempting to replicate the functionality of jQuery's string-based HTML element determination, I employed a split function. However, rather than returning a list of values as intended, it produced an array containing twenty empty strings. console.l ...

Unable to trigger AJAX POST with jQuery click handler

Important Note: Personally, I have a preference for utilizing jQuery over the shorthand $; although it involves more typing, I find it to be more readable. I am working on a simple form that allows users to input their first and last names as well as an e ...

What is the best way to select an element that is currently visible but hidden underneath another element?

I have developed a circular graphic using primarily HTML and CSS, with some JavaScript and JQuery functionalities incorporated for text curving and planned interactions in the future. However, I've encountered an issue where clicking on the upper rig ...

Tips for effectively implementing Ajax functionality within a table grid

As someone who is fairly new to programming, I have a question regarding implementing basic operations in a grid table. Should I use AJAX for these operations or can I achieve them without it? In the grid, each record has operations such as view, edit, up ...

Close the overlay by clicking outside of it

I'm working on creating a unique pop-up window that appears when a customer adds a product to their cart. The design features red and green background divs with a darker overlay (#overlay-daddy) and a white child div (#overlay). My issue arises from ...

Toggle the selections of items from different lists when any item is selected, excluding the current selection

I am currently facing an issue with using two selectable lists that have the same selectable class. My goal is to toggle all items regardless of their parent list when a selection is made. However, at the moment, when I select an item, only the selections ...

Managing numerous requests simultaneously without causing one to delay or block the others

I am facing challenges when it comes to managing multiple client requests. Ideally, each route should be handled asynchronously, but I am struggling to handle one request after another without the previous one interfering with the next one. In the code sni ...

Looking for a solution to resolve the issue of a jQuery function that only works once? This problem is directly related to using

I'm experiencing an issue with my jQuery code where it only works once in a fresh browser session. After that, the e.preventDefault() function stops working and the entire code fails to run. I suspect this issue may be related to the .load AJAX funct ...

Content within the divs can now be scrolled vertically. Refresh the page to return to

I'm using the simplest HTML 'overflow: scroll' type mark-up for creating a scroll bar within multiple div containers to display text and image content within the scroll. I have it setup within tabbed content areas, that are pulled together c ...

Retrieving PHP post request data with Jquery AJAX

Lately, I've been struggling to retrieve data from PHP using JQuery's AJAX function. While sending the request is successful, receiving the information is where I'm facing a challenge. Any help would be greatly appreciated. JQuery: $.ajax( ...

Troubleshooting problem related to configuration conflicts in terser-webpack-plugin parameters

I am facing an issue with my Terser configuration, causing the compressed version of my page to break. The problem arises from functional declarations in my index.js that need to be accessible from Bootstrap modal windows loaded at a later time. For insta ...

When utilizing Jquery.Ajax, I noticed that it automatically includes double quotes and new line characters in my

After upgrading my dev server from PHP 5.4 to PHP 5.6, I encountered a peculiar error during testing. The issue arises when my PHP code returns data as an array or string through my main View, which then uses the following code snippet to send the data bac ...

Is it possible to utilize bootstrap sizing by considering the size of the containing div?

Setting the Scene Imagine a scenario where two divs are placed side by side. div1: 500px wide div2: taking up the remaining width The goal is to make the items in div2 responsive using Bootstrap so that classes like mt-sm-3 can be utilized for differen ...

Retrieving precise information from the backend database by simply clicking a button

As a new full stack programmer, I find myself in a challenging situation. The root of my problem lies in the backend table where data is stored and retrieved in JSON format as an array of objects. My task is to display specific data on my HTML page when a ...

Fade in a fresh background using jQuery upon clicking

Although there are similar discussions on Stack Overflow, none of them seem to fit my specific situation. I have a body background that I would like to change when a certain element is clicked. $('#start').click(function(){ $(this).fadeOut( ...

The el-dialog is functioning properly, however, I am looking to add CSS that will animate it to open

I've set up a dialog to open on button click, and it's functioning correctly. My goal is to have the dialog open from the bottom with a height of 300px, show the contents inside it, and then hide when I click outside or inside any element. Here ...

Crafting a horizontal sub-menu that sits between the parent element and the company logo

Seeking assistance with designing a navigation menu that places the navigation bar above the site logo. I envision the sub-menu expanding horizontally from the navigation bar and positioning itself between the parent navigation bar and the logo. The sub- ...