Accordion styling using CSS

My current issue lies with setting the style for the contents of an accordion. The problem arises when the styles defined in a CSS file do not take effect on the content of my accordion when using the following code along with a separate CSS file:

<script>
    $(document).ready(function() {
    var str = '<h3><a href="#">Device</a></h3>' +
            '<div class="content">'+
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>' +
          '<h3><a href="#">VM</a></h3>' +
            '<div class="content">'+
                '<div class="block left" >' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>';

The relevant code snippet from the CSS file is as follows:

div.toolbox-block {
    width:30px;
    height:30px;
    overflow: hidden;
    background-color: rgba(0,0,0,1);
}
div.left {
    float:left !important;
}

The above code works perfectly if I set the styles inline to the tags within the variable 'str', like this:

str = '<h3><a href="#">Device</a></h3>' +
            '<div class="content">'+
                '<div style="width:30px; height:30px; overflow: hidden; background-color: rgba(0,0,0,1); float: left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div style="width:30px; height:30px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>' +
          '<h3><a href="#">VM</a></h3>' +
            '<div class="content">'+
                '<div style="width:30px; height:30px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div style="width:30px; height:28px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>';

Answer №1

Have you considered a different approach to including the accordion markup? Instead of dynamically adding it through Javascript, you could include it in your HTML file and initially hide it with CSS. Then, use jQuery on document ready to make it visible.

Here's an example:

<div id="accordion" class="hidden">
    <h3><a href="#">Device</a></h3>
    <div class="content">
        <div class="block left">
            <img width=30 height=30/>
        </div>
        <div class="block left">
            <img width=30 height=30/>
        </div>
    </div>
    <h3><a href="#">VM</a></h3>
    <div class="content">
        <div class="block left">
            <img width=30 height=30/>
        </div>
        <div class="block left">
            <img width=30 height=30/>
        </div>
    </div>
</div>

And your javascript:

<script>
$(document).ready(function() {
    $('#accordion').removeClass('hidden') // removes default CSS class to make it visible
        .accordion(); // turns it into an accordion
});
</script>

This way, the accordion markup is not generated by Javascript and should work with your CSS rules.

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 process of unescaping characters in jQuery.post() operations

I am currently working on a website that allows users to submit code, which is then processed by a PHP script and saved onto the server. To send the code to the PHP script, I am utilizing jQuery's $.post() method. However, I am encountering an issue w ...

Incorporating a complex React (Typescript) component into an HTML page: A Step-by

I used to have an old website that was originally built with Vanilia Javascript. Now, I am in the process of converting it to React and encountering some issues. I am trying to render a compound React(Typescript) component on an HTML page, but unfortunatel ...

Choosing the anchor text in the second td element of a table using jQuery

I have a webpage with the following HTML structure... <input type=text id="search"> <table> <tr> <td></td> <td><a href="">Some text...</a></td> <td></td> </tr> </table> This co ...

Struggling to designate the active button on an HTML/CSS webpage

As a beginner in the world of HTML and CSS, I'm facing some challenges with making buttons appear active. My goal is to have button1 highlighted by default when it's selected, and upon clicking on button2, the content above should change successf ...

What is the best way to transfer an ID to a dropdown selection list?

Whenever a user chooses an option from a drop-down menu, an event needs to be triggered. I have a checkbox that retrieves an ID from the database; based on this ID, a user can be added or removed from the drop-down menu. var ajReq = new XMLHttpRequest(); ...

Optimizing HTML/CSS performance: Comparing flexbox and tables for handling extensive datasets

Creating a React table component with hundreds of lines and variable cell widths can be complex. Would it be better to implement this using flexbox or a traditional table structure in HTML/CSS? ...

tips for making a row stand out in a massive table with jquery

When changes are made to data rows on tables or any other item with multiple rows of data, I have observed that the edited row temporarily turns yellow before returning to its original state. In my case, I have a data table with approximately 500 rows. A ...

Displaying or concealing fields through Javascript depending on the category type of the results item

I am facing an issue with displaying certain fields in each individual property search result based on the property type. For example, if a property is classified as Land, I do not want the bedrooms and bathrooms fields to be visible, but if it is a Villa, ...

Unusual phenomenon in ASP.NET: Adding Debug=true in the web.config file results in CSS modifications

As the question suggests: After including debug=true in my web.config file (without making any other alterations to the code), my page is displayed without a background color. Can anyone shed light on why this might be happening? ...

evaluation is not being executed in javascript

I am struggling to assign a value of 1 to the educationflag variable. I am trying to avoid calling the enableEdit.php file when the flag is set to 1. The issue arises when control reaches the if condition but fails to set the variable to 1. Here is my cod ...

Unleash the power of JQuery by capturing multiple events simultaneously, yet

What is the best way to handle multiple events like change and input, but only trigger one event at a time? For example: When an input event occurs, trigger the input event without triggering the change event. When a change event occurs, trigger the cha ...

Spice Up Your Website with a Unique Twist on Bootstrap Ajax Tabs

I am facing an issue with achieving a particular result. When a user navigates through the tabs, they do not see the loading effect when returning to the same page (this is because the default value is overridden by the loader). How can I override the .loa ...

Sending data from jQuery to an AngularJS function is a common task that can be accomplished in

In my Controller, I have a function that saves the ID of an SVG path into an array when a specific part of the image.svg is clicked. $(document).ready(function(){ var arrayCuerpo=[]; $('.SaveBody').on("click", function() { ...

Comparing AngularJS $http with $http.post: A Detailed Analysis

As a newcomer to AngularJS, I am experimenting with sending data to the server and mapping it to a DTO in a REST Service. Here's how I attempted to do it: saveUserUrl = "http://localhost:8080/App/rest/UserManager/saveUser"; var res ...

Chrome clipping positioned spans

Having trouble positioning a label above inline sections with spans in Chrome, as the labels are getting clipped oddly. Check out these screenshots: Firefox view: Chrome view: In the Chrome screenshot, you can see that the labels are being cut off based ...

Creating customized JavaScript using jQuery for Drupal 7 Form API

Currently, I am working on developing a custom form using Drupal 7 with form API and a tableselect that includes checkboxes. I have some specific constraints for selecting the checkboxes that I intend to implement using jQuery. I created a function for han ...

Is there a way to prevent a .load() function from executing if it has

On my webpage, I have implemented multiple buttons that each load a different partial "view" using the jQuery .load() method. While this functionality works well, I observed that repeatedly clicking on one specific button results in the partial view being ...

Navigating events within the Material Design Light (MDL) mdl-menu component can be

I am utilizing Material Design Light with just PHP, jQuery, and MDL min CSS and JS, no other frameworks involved. Keeping it simple with a basic menu. <button id="lang-switcher" class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect"> ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...

Creating a custom function that targets a specific element

When working with JavaScript, you may often encounter functions written in the form of document.getElementById("element").someFunction(); or $("element").someFunction(); rather than the traditional someFunction($("element"));. This is similar to a function ...