Converting CSS into jQuery

I am exploring ways to create a collapsible section with arrow icons (right and down arrows) in jQuery or JavaScript. Can anyone provide guidance on how to convert my CSS styling into jQuery code?

Below is the jQuery approach I have attempted:

$(document).ready(function(){
    $(".collapse").on("hide.bs.collapse", function(){
        $(".arrows").after().css({"content":"\e080","font-family": "Glyphicons Halflings"});
    });
    $(".collapse").on("show.bs.collapse", function(){
        $(".arrows").after().css({"content":"\e114","font-family": "Glyphicons Halflings"});
    });
});

Here's the corresponding CSS code:

.arrows:after {
    font-family: "Glyphicons Halflings";
    content: "\e114";
}

.arrows.collapsed:after {
    font-family: "Glyphicons Halflings";
    content: "\e080";
}

Lastly, here is the HTML structure being used:

<div class="panel-group">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapse1">Collapsible panel <span class="arrows"></span></a>
      </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse">
      <div class="panel-body">Panel Body</div>
      <div class="panel-footer">Panel Footer</div>
    </div>
  </div>
</div>

Answer №1

Make sure to use this code snippet which toggles the class on hide and show events

$(".collapse").on("hide.bs.collapse", function() {
  $(this).parent().find('.arrows').addClass('collapsed');
});
$(".collapse").on("show.bs.collapse", function() {
  $(this).parent().find('.arrows').removeClass('collapsed');
});
.arrows:after {
  font-family: "Glyphicons Halflings";
  content: "\e114";
}
.arrows.collapsed:after {
  font-family: "Glyphicons Halflings";
  content: "\e080";
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <div class="panel-group">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapse1">Collapsible panel <span class="arrows collapsed"></span></a>
      </h4>
      </div>
      <div id="collapse1" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
        <div class="panel-footer">Panel Footer</div>
      </div>
    </div>
  </div>
</body>

</html>

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

Tips for including vue-autonumeric in your webpack 2 setup

Encountering a problem while bundling the vue-autonumeric package with Webpack 2, where the dependency AutoNumeric is not being found properly. Although there is an alias set up in the configuration that works fine with webpack 3, it seems to fail when wo ...

Error: Value not defined in the (Node, Express, Pug, JQuery) environment

I'm encountering a common issue as a beginner and could really use some assistance. I have tried multiple solutions but still can't resolve the error "ReferenceError: $ is not defined" when attempting to use jQuery. My project structure looks lik ...

Confusing directions about parentheses for "this.fn.bind(this)(super.fn(...args)"

While reviewing a project, I came across code that can be simplified to: export abstract class Logger { private static log(level: LogLevels, ...args: Array<any>) {/**/} public error(...args: Array<any>): LogData { return Logger ...

Tips for preventing the repeated loading of external libraries: Leveraging npm packages, using browserify, and strategically implementing script tags

I have developed a single-page web app using jQuery along with another library and a bundle.js created with npm and browserify. The bundle.js also relies on the same libraries. My architecture works like this: On the HTML page, I load certain functiona ...

Issue with scrollTop functionality on Android smartphones

Currently working on implementing chat functionality for an Android mobile app using jQuery and jQuery mobile theme for the frontend. The issue I am encountering is related to the scrollTop() function not functioning as expected in Android browsers. Does ...

Ways to update the title of a Magento widget

I've added a widget to showcase new products on my Home page and it's displaying perfectly. However, I want to get rid of the title "New Product" that comes with it. This pesky little thing needs to go! https://i.sstatic.net/P0NeS.jpg I'm ...

Provide props to vue-router along with boostrap-vue's b-nav-item

I am currently in the process of transitioning my SPA from modals that load components to routed pages that load components. I have been able to successfully open a page using to="/fixtures" in the html, but I am facing an issue with passing in a component ...

Sending cookies via POST/GET request with Credentials is not functioning

Greetings, despite the numerous duplicates of this inquiry, my attempt to solve it has been unsuccessful. Therefore, I am initiating a fresh discussion. Aside from utilizing axios, I also experimented with fetch but encountered similar outcomes. On the b ...

Protractor is unable to interact with the embedded <span> within the <a> tag

I am facing an issue where I have two nested <span> elements inside an <a> tag. My objective is to trigger a click event on the second <span>. I tried using the by.id method on the custom classes I created, but it did not work. I also att ...

Manipulating arrays in JavaScript through HTML processing

I'm encountering an issue while trying to send an array to a JavaScript function. Here's what I have so far: Within the controller: @data = [{date: '2014-08-17'}, {date: '2014-08-20'}].to_json In the view: <%= content_t ...

A useful tip for emphasizing tags that have attributes with endings

Here is the HTML list I'm working with: <li class=​"info" data-info=​"" data-title=​"info 1" data-info-id=​"222643">…</li> <li class=​"info" data-info=​"" data-title=​"info 2" data-info-id=​"217145">…</li> ...

"Troubleshooting: Issue with AngularJS ng-repeat not functioning properly when using an

I am working with a simple list <ul> <li ng-repeat="spiel in spielListe">Do something</li> </ul> Along with a perfectly connected controller $scope.spielListe = []; There is also a method that adds objects to the array in th ...

Sharing data between two PHP pages via an AJAX request

On my webpage, specifically on page 1.php, I am saving a variable to an input field text. Name:abc Done. After clicking the 'done' button, the name abc is sent to another page called 2.php via an Ajax request. In 2.php, I am storing the value ...

What is the best way to showcase page content once the page has finished loading?

I'm facing an issue with my website. It has a large amount of content that I need to display in a jQuery table. The problem is that while the page is loading, all rows of the table are showing up and making the page extremely long instead of being sho ...

Animating an image inside a bordered div

I'm attempting to create an animated effect where an image moves randomly within the boundaries of a div container. While I've come across solutions for animating within borders, none have specifically addressed keeping the image inside the div. ...

Creating Production Files for Web using RxJs and TypeScript

I am interested in developing a JavaScript Library using RxJs (5.0.0-Beta.6) and TypeScript (1.8.10). My TypeScript file is successfully compiling. Below are the files I have: MyLib.ts: /// <reference path="../../typings/globals/es6-shim/index.d.ts" ...

Are programmatic clicks distinguishable from manual clicks in JQuery?

Currently, I am in the process of creating selectable and draggable elements for a table. You can test the functionality by following this JsFiddle link (please try vertically selecting). Initially, the selection works well but upon trying to select for th ...

Enhance the CSS styling for the React-Calendly integration in a React project

I am trying to customize the CSS of an Inline Widget called React Calendly. I have attempted to use React Styled Component Wrapper, Frame React Component, and DOM javascript but unfortunately, the design changes are not reflecting as desired. Specifically, ...

React's .map is not compatible with arrays of objects

I want to retrieve all products from my API. Here is the code snippet for fetching products from the API. The following code snippet is functioning properly: const heh = () => { products.map((p) => console.log(p.id)) } The issue ari ...

Displaying content on the <div> element

Looking for recommendations for a jQuery plugin or JavaScript solution that allows me to load a full "view" into a <div> when a user clicks on a link. The challenge I'm facing is that I have 8 pages, with the Homepage consisting of 3 divisions: ...