Tips for concealing collapsible-header while collapsible-content is visible in Jquery Mobile

I am currently working with Jquery Mobile and utilizing a collapsible set. By default, when I click on h3, the collapsible content is shown. What I am trying to achieve is that when I click on h3 (Animals), the h3 element should disappear and only the ul containing Cats should be visible. Similarly, if I click on the image inside the ul for Cats panel, it should be hidden and the header-h3 should reappear. This functionality needs to be applied across a large dataset.

JSFIDDLE

//html
    <div id="list" data-role="collapsibleset" data-inset="false">
        <div data-role="collapsible">
            <h3>Animals</h3>
            <ul data-role="listview" data-inset="false">
                <li><span><img id="image" src="http://lorempixel.com/20/20/"></span>Cats</li>          
            </ul>
        </div>

Answer №1

One way to hide the h3 element using CSS and then show it again along with hiding the child ul when a specific image is clicked is by utilizing jQuery click handlers. Here's an example:

$(function () {
    $('div[data-role="collapsible"] > h3').click(function() {
        if(!$(this).is(':hidden'))
            $(this).hide();
    });
    $('ul[data-role="listview"] img').click(function() {
        $(this).closest('div[data-role="collapsible"]').children('h3').trigger('click').show();
    });
});

Check out the updated jsFiddle, which was forked from your original one: http://jsfiddle.net/dBH3h/

The first click handler targets the

div[data-role="collapsible"] > h3
to handle the hiding of the h3 element when expanding it. The IF condition within this handler sets up the next action.

By adding a click event listener to the ul[data-role="listview"] img, we are able to use basic DOM traversal in jQuery to locate the corresponding h3, trigger its click event (which automatically hides the ul and shows the expand icon), and then display it again.

UPDATE: It's important to note that this method may lead to unexpected outcomes with this specific collapsible widget as it does not support multiple open panels simultaneously. For widgets where this limitation doesn't apply, this solution should work fine - however, for this widget, consider exploring Omar's alternative answer.

Answer №2

When the `collapsibleexpand` event is triggered, we can hide the header by adding the `ui-screen-hidden` class. To display the header again, an event needs to be bound to the collapsible's content. For example, a `click` event can be used to show the header again and programmatically collapse the content using `.collapsible("collapse")`.

$(document).on("collapsibleexpand", "[data-role=collapsibleset]", function (e) {
   // Show previously hidden headers
   $(".ui-screen-hidden").removeClass("ui-screen-hidden");
   // Hide the target collapsible's header
   $("h3", e.target).addClass("ui-screen-hidden");
   // Listen for click event on collapsible's content
}).on("click", ".ui-collapsible-content", function () {
   // Retrieve parent collapsible 
   var collapsible = $(this).closest(".ui-collapsible");
   // Show header
   collapsible.find(".ui-screen-hidden").removeClass("ui-screen-hidden");
   // Collapse the target collapsible 
   collapsible.collapsible("collapse");
});

Check out the demo here

Answer №3

To achieve the desired effect, you can simply include the following CSS:

.custom-collapsible h3 {
    display: none;
}

.custom-collapsible.collapsed h3 {
    display: block;
}

Additionally, add the below JS function:

$(".custom-collapsible-content").click(function(){
    $(this).parent().collapsible("collapse");
});

You can test this functionality by visiting this fiddle.

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

The lack of synchronization between the updated state in one function and its counterpart is causing discrepancies, resulting in the incorrect information being sent to the API

Whenever I press the following button: <Button onClick={(e) => { addToCard(item); handleprisma(); }} > add to cart </Button> This function is meant to add the item to my shopping cart: const addToCard = (item) => { co ...

Why is T r showing an error when l c.fireWith $.AJAX.error?

This response mentions that the issue occurs when it sends twice. In my case, the problem arises when a user modifies the value in a selection box, so it's not related to that particular scenario. If you check out this link, you'll find my prev ...

Is there a way to incorporate text fields and input in BootstrapDialog?

Is it possible to include text fields and inputs within this modal using BootstrapDialog without modifying the library? Additionally, can I use $.ajax() inside? <button class="btn btn-primary" id="add_item">New item</button> jquery $('# ...

The Three.js scene is failing to render properly on subsequent attempts

Currently, I am in the process of developing a web-based height map generator using Three.js. The project involves utilizing multiple canvases to display the generated height map, individual octaves that make up the map, and a separate canvas to showcase h ...

Is there a way to automatically populate the result input field with the dynamic calculation results from a dynamic calculator in Angular6?

My current challenge involves creating dynamic calculators with customizable fields. For example, I can generate a "Percentage Calculator" with specific input fields or a "Compound Interest" Calculator with different input requirements and formulas. Succes ...

What is causing the child table (toggle-table) to duplicate every time a row in the parent table is clicked?

After creating a table containing GDP data for country states, I've noticed that when a user clicks on a state row, the child table displaying district GDP appears. However, there seems to be an issue with the child table as it keeps repeating. I&apos ...

jQuery - Generating numerous instances

Currently, I am utilizing a jquery plugin for file uploads found at the following link: While the plugin works smoothly for a single upload box, I have attempted to set up multiple upload boxes by switching the ID to a class without success. Here is the ...

Text pushing upper div caused by height increase of inner input in grid column due to excessive text length

To truly grasp the issue I'm facing, it's best to watch the following videos: video of content inside input pushes up div of messages - NOT WORKING If you want to see how I envision it working correctly, check out this video from discord.com ch ...

Developing pagination functionality in ReactJS

I came across this piece of code in a forum (how to implement Pagination in reactJs): constructor() { super(); this.state = { todos: ['a','b','c','d','e','f','g','h ...

Repairing a syntax error in a jQuery selector variable

$(".className").click(function(){ var link = $(this).find("a").attr('href'); //output is '#myID' var findItems = $(link '.mydiv').length; //WRONG var findItems = $(link + '.mydiv').length; ...

Linking information within a three-dimensional mesh structure in the context of a city created through procedural generation

Currently, I am working on developing a procedurally generated city using three.js, focusing mainly on creating buildings. One of the crucial requirements for this project is that when a user clicks on a building with their mouse, detailed information abou ...

Different Ways Split Button Format Can Vary Based on Web Browser

I am encountering a formatting issue with a jQuery splitbutton on my webpage. In order to adhere to my company's design standards, I am essentially converting a link into a button. The functionality works perfectly fine; however, depending on the brow ...

Tips on how to loop a song continuously in jPlayer's circular mode

Can you help me figure out how to make a song repeat in jPlayer circle? I have the code for autoplay, but I also want to add a repeat functionality. I tried adding options like repeat:, but it didn't work as expected. <script type="text/javascript ...

PHP code to create a bottom border in echo statement

Is it feasible to create a bottom border using echo? I'm inquiring about this because I haven't been able to find a direct answer from a reliable source. Essentially, what I want to accomplish is creating a notification similar to those on Faceb ...

Show Pop in relation to modified Text

When the user clicks on the text, a pop-up is displayed after the last word in the text.... https://i.stack.imgur.com/VWQCa.png The logic being used is : left = layer.width + layer.x Code : document.getElementById(lightId).style.left = layer.x + docume ...

The difference between using document.getElementsByTagName("img") and document.getElementById('testimg')

My HTML code is as follows: <a href="index.php"><img id="testimg" src="images/logo.png"/></a> This is my JavaScript code: function getW(){ var theImg = document.getElementById('testimg'); return theImg; } theImg = ...

Having trouble with implementing the mottie jQuery keyboard on a popup box?

I am facing an issue with implementing the mottie jQuery keyboard on a popup window. The keyboard functions properly on the main page but does not show up on the popup. I believe there is a mistake in my implementation, but I can't seem to identify it ...

javascript identify dissimilarities within arrays

Working on an Angular 2 application and attempting to identify the difference between two arrays (last seven days and missing dates within the last seven days). Everything works fine when initializing the array through a string, like in example code 1. How ...

Ensuring a height of 100% with the utilization of two divs positioned using float left and right, along with

It's really quite simple: HTML: <div> <section class="left"> </section> <section class="right"> </section> <div class="clear"></div> </div> CSS: div ...

Utilizing the information from a file as the data for an object

I'm struggling to incorporate data from a .json file using formData in an HTTP request. I'm having trouble adding this data into the options object correctly. {"name":"charlie","age":"20"} Here is what I ...