"Utilize jQuery to create a dynamic animation by togg

Looking for guidance on how to add animation to gradually reveal the answer? Provided my code below. Any assistance in making this functional would be greatly appreciated.

<div class="q-a-set">
<div class="licensing-question" id="q_1">
<i class="fa fa-caret-square-o-down" aria-label="Answer"></i> <span>Do I need to pay?</span>
</div>
<div class="licensing-answer-closed" id="a1"><span class="licensing-answer">Yes</span></div>
</div>

.licensing-question{
        font-size: 2.2em;
        font-weight: 700;
        font-family: 'Source Sans Pro', sans-serif;
        color: #a8a8a8;
        cursor: pointer;
        line-height: 1.2em;
        margin: 0;
        padding: 0;
        display: inline;

    }
    .licensing-answer-closed{
        height: 0;
        visibility: hidden;
    }
    .licensing-answer{
        color: #222222;
        font-size: 1.8em;

    }

var showAnswerBtn = $(".licensing-question");
var caret = showAnswerBtn.find('.fa');


showAnswerBtn.click(function () {
    //alert(caret);
    event.preventDefault();
    var target = this.id.split('_');
    var id = "a" + target[1];

    $("#" + id).toggleClass('licensing-answer-closed');
    $(caret).toggleClass('fa-caret-square-o-up');

});

Answer №1

To achieve a smooth transition effect, you can utilize CSS properties like transition combined with opacity. By adding a specific class to your HTML elements, you can easily apply the transition effect using a class selector.

var toggleButton = $(".licensing-question");
var caretIcon = toggleButton.find(".fa");

toggleButton.click(function() {
  event.preventDefault();
  var targetId = this.id.split("_");
  var answerId = "a" + targetId[1];

  $("#" + answerId).toggleClass("licensing-answer-closed");
  $(caretIcon).toggleClass("fa-caret-square-o-up");
});
.licensing-question {
  font-size: 2.2em;
  font-weight: 700;
  font-family: 'Source Sans Pro', sans-serif;
  color: #a8a8a8;
  cursor: pointer;
  line-height: 1.2em;
  margin: 0;
  padding: 0;
  display: inline;
}
.licensing-answer-closed {
  height: 0;
  opacity: 0;
}
.licensing-answer {
  color: #222222;
  font-size: 1.8em;
}
.licensing-answer-parent {
  transition: opacity 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="q-a-set">
  <div class="licensing-question" id="q_1">
    <i class="fa fa-caret-square-o-down" aria-label="Answer"></i> <span>Do I need to pay?</span>
  </div>
  <div class="licensing-answer-closed licensing-answer-parent" id="a1"><span class="licensing-answer">Yes</span></div>
</div>

Answer №2

element, make sure to exclude the # symbol when setting the id variable, but include it in your selector.
var id = "a" + target[1]; // remove -> "#"
$("#" + id).toggleClass('answer-closed');

Apply the same approach for the "caret".

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

Class for Bootstrap CSS to adjust card image when resized

I'm stumped, I can't seem to find the right CSS element to trigger the transition from displaying the image on the right to below the text. It's a simple change that I didn't anticipate, but I kind of like it. In horizontal view, I wan ...

Why do I keep encountering the error of an undefined variable? Where in my code am I making a mistake

I am struggling to troubleshoot an issue with creating a simple ease scroll effect using the jQuery plugins easing.js and jquery-1.11.0.min.js. $(function(){ //capturing all clicks $("a").click(function(){ // checking for # ...

D3.js Issue: The <g> element's transform attribute is expecting a number, but instead received "translate(NaN,NaN)"

I encountered an error message in my console while attempting to create a data visualization using d3. The specific error is as follows: Error: <g> attribute transform: Expected number, "translate(NaN,NaN)". To build this visualization, I ...

Node.js and Express facing challenge with Stripe checkout functionality

I am encountering an issue while attempting to integrate stripe into my checkout process. When I click on the checkout button, instead of being directed to the checkout page, I receive the following message: {"url":"https://checkout.stripe.c ...

The animated loading image is taking an eternity to actually load

My website is loaded with heavy front-end features and I'm using a loading gif to help with the loading process. However, I've noticed that in Safari, there is a delay before the gif loads after the background does, which takes away from the inte ...

How can we dynamically render a component in React using an object?

Hey everyone, I'm facing an issue. I would like to render a list that includes a title and an icon, and I want to do it dynamically using the map method. Here is the object from the backend API (there are more than 2 :D) // icons are Material UI Ic ...

Utilize JavaScript to enclose every piece of text within single quotes in a span element

I have a tiny snippet of text: "some sample text" just a little more "additional text" I am trying to figure out how to wrap all the content between quotation marks in a span container, similar to what hilight.js does. I've been struggling to make it ...

Choose elements with unique identifiers other than those provided by the API

Seeking assistance with Material UI select element and naming dropdown. Have a few questions: 1: My API that populates the dropdown lacks good names. Is it possible to assign names like 'data 1, data 2, data 3...' to the options without manually ...

Execute a task on the elements contained in an array within a collection of arrays

I'm grappling with a challenge that involves calculating the sum of the products of two elements that are housed in separate objects. Essentially, I'm seeking to determine TotalValue through the formula: TotalValue = Sum[Arrays.close * List.amtP ...

Sending Ajax GET request following the first Ajax POST request

Upon completing the initial ajax post, I am attempting to make a second ajax call. The initial call is a POST and the second call is a GET. Even though I am able to retrieve the values from the url file.php in the second call, I am facing difficulties in u ...

Is there a way to verify an email address and transfer form data to a different HTML page for printing?

How do I troubleshoot email validity checking issues in my form? It works fine when no characters are entered, but fails when characters are inputted. What could be causing this problem? Additionally, I want to open a new HTML sub-file after form submissi ...

Testing a directive that contains a parent directive declaration is essential in ensuring proper functionality within an

I recently developed a utility tool for my application called batch-checkbox-util.js and I am eager to conduct some tests on it. Although I have written a test script, which can be found at the same link provided, I am encountering an issue during the tes ...

When scrolling, the text or logo inside the navbar seems to dance with a slight wiggling or

I recently implemented a code to create a logo animation inside my navigation bar that expands and contracts as I scroll, inspired by the functionality of the Wall Street Journal website. However, this implementation has caused some unintended side effects ...

"Integrate checkbox functionality in datatables to dynamically toggle the WHERE clause

Currently, my datatable is being populated successfully with ColdFusion and the sorting is working perfectly. One of the columns in the SQL table is the status. At the moment, I am fetching all the results where the status is not 'Completed'. Al ...

Interop-require-default is nowhere to be found in the babel-runtime

I'm really stuck on how to resolve this error. I've tried searching online and followed the suggestions given by others. I even went as far as deleting 'node_modules' and reinstalling, but nothing seems to be working. The specific erro ...

"Exploring the Mini Drawer feature on Material UI brings up a whole new page for the Link

Currently, I am working on a project that involves updating stocks using Material UI. I have implemented a mini drawer from Material UI, but when I click on the menu link, it routes to a new page instead of rendering on the homepage itself. In my App.JS f ...

Adjust the horizontal background-position transition for color change while scrolling using jQuery and CSS

I came across a fascinating website - - where the background-position changes on scroll() from a linear gradient with two colors to smoothly slide one color off the screen. While I grasped how they achieved the split colors using linear-gradient, I'm ...

Interactive Animation with Three.js Curved Path

I am attempting to animate a 2D curve in Three.js gradually. Because I will require more than 4 control points, I have decided against using Bezier curves and instead opted for a SplineCurve. When I check the position of geometry.vertices of my line, I no ...

Angular 7 - Issue with Loading Material Component Styles

In the midst of my latest Angular 7 project, I decided to incorporate the out-of-the-box material components. Unfortunately, there seems to be a hiccup with some of the CSS not being applied and pinpointing the cause is proving to be an elusive task. For ...

Starting a checkbox group with values based on total number

I am facing a challenge with a group of checkboxes that have values assigned as 1,2,4,8,16,32,64. Each number corresponds to a day of the week (e.g. Sunday, Monday etc...) The total value of the checkbox group is calculated as the sum of the selected chec ...