What is the best way to update font-awesome caret from caret-right to caret-down?

I've encountered an issue with the code snippet below. When I replace fa-folder with fa-caret-right and fa-folder-open with fa-caret-down, the functionality stops working, and I can't figure out why. Any assistance on this matter would be highly appreciated. Thank you!

<html>
<head>
<style>
    .accordion {
    cursor: pointer;
    }
    .panel {
    display: none;
    }
    .panel.show {
    display: block;
    margin-left: 20px;
    }
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
</head>
<body>
    <p class="accordion"><i class="fa fa-folder" aria-hidden="true"></i>&nbsp;&nbsp;Building Guides</p>
    <div class="panel">
    <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>&nbsp;&nbsp;Accessory Structures Building Guide 2012</p>
    <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>&nbsp;&nbsp;Basement Finish Building Guide 2012</p>
    <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>&nbsp;&nbsp;Decks Building Guide 2012</p>
    </div>
<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
    this.nextElementSibling.classList.toggle("show");
    this.firstChild.classList.toggle("fa-folder-open");
    }
}
</script>
</body>
</html>

Answer №1

To achieve the desired effect, remember to deactivate the right class when activating the down class. I have also removed non-breaking spaces and utilized CSS for a more visually appealing outcome.

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.nextElementSibling.classList.toggle("show");
    this.firstChild.classList.toggle("fa-caret-right")
    this.firstChild.classList.toggle("fa-caret-down");
  }
}
.fa {
  width: 20px;
}

.accordion {
  cursor: pointer;
}
.panel {
  display: none;
}
.panel.show {
  display: block;
  margin-left: 20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<p class="accordion"><i class="fa fa-caret-right" aria-hidden="true"></i>Building Guides</p>
<div class="panel">
  <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>Accessory Structures Building Guide 2012</p>
  <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>Basement Finish Building Guide 2012</p>
  <p><i class="fa fa-file-pdf-o" aria-hidden="true"></i>Decks Building Guide 2012</p>

Answer №2

The reason for the confusion is due to a common spelling error. The correct term is caret, not carat.

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

Exploring the semantic-ui dropdown menu feature in Chrome

When I first started learning to code with semantic-ui framework, I noticed a difference in behavior in Chrome compared to other browsers. The pointing menu on the left element (such as the search box) breaks down in Chrome, whereas it displays correctly i ...

Holding off on routing the pathName through the router until the next-page-transitions have been completed

In my current setup, I have two distinct elements at play. Using the code snippet below: className={router.pathname.startsWith("/pagename") ? "menu-active" : ""} I am able to apply the menu-active class to the pagename naviga ...

How can I trigger onClick using VBA within HTML script?

Is there a way to trigger the onClick event in an HTML page? I found this code by searching on Google: <img src="Imagens/lupa.gif" alt="Search Folder" class="img" onclick="locate();"> Currently, this is my VB ...

Updating token (JWT) using interceptor in Angular 6

At first, I had a function that checked for the existence of a token and if it wasn't present, redirected the user to the login page. Now, I need to incorporate the logic of token refreshing when it expires using a refresh token. However, I'm enc ...

Translating Angular strings from identical origins

I am currently utilizing angular-translate within my project. Within my views, there are several strings that I would like to translate, such as <ul> <li ng-repeat ="title.value as title in vm.states"> </ul> The array vm.states contai ...

The div block containing the table is experiencing horizontal overlap as additional elements are inserted

When creating a table within the div section of the code, I am incorporating text using the DRAG and DROP feature with appropriate styling. The table adjusts in size when I resize my window, indicating that it is functioning correctly. However, as the num ...

Trouble with activating the full screen feature on YouTube embedded in an HTML iframe

I created a website with a YouTube video, and in order to have a separate thumbnail, I used the following code: <div class="embed-responsive embed-responsive-16by9 youtube" style="height: 130px; width: 240px; " src="http://molugu.com/yantraev/divayou ...

positioning a text input in the center instead of at the top of the page

Hello everyone, I am just starting out with HTML and I have a question. How can I make the Username: <input type="text" name="Username" style="- margin-top: 200px;"> appear in the center of the page instead of at the top? I've tried changing the ...

Utilizing 'Ng-If' causes a glitch in the program during the execution of a $( '#x' ).change event or when adding a new item with AngularFire $add

After implementing ng-if in my code, I observed two specific instances where it caused unexpected behavior. The first instance involves using ng-if="isOwnProfile" for an image-upload toolbar. However, the use of ng-if resulted in the event listener ceasin ...

Combining Gulp with Browserify using Globs

After following the recipe from the official gulp repo for using browserify with multiple entry points, everything worked smoothly when I only had one file. However, now that I am trying to run the task for multiple files, it displays the following tasks ...

Select elements that do not have a specific parent class using LESS

I have created an HTML structure as outlined below: <ul class="list list--sm"> <li class="list__item list__item--draggable"> List item 1 <ul> <li class="list__item list__item ...

The layout generated by Metronic does not display the CSS styles

I've been working on creating a Metronic layout using the layout builder, but I'm running into an issue. Whenever I try to open index.html, all I see is this: screenshot It appears that the CSS styles are not loading properly. Interestingly, wh ...

Trouble encountered in adjusting the alignment of labels

I'm struggling with aligning my links properly. https://i.sstatic.net/W5G4Z.png I want Yesterday This week This month to line up perfectly with the DateTime From and Last 2 days Last 7 days Last 30 days labels for DateTime To. This is how it appear ...

Angular 2: Dealing with NaN values from Snapshot Parameters and Services

I am facing difficulties in transferring parameters from one component to another using Angular2 router and activated route. Here is my API Service: getModels(makeNiceName: string): Observable<Models> { return this.http.get(this.baseURL + mak ...

What's the best method for creating a wraparound drop shadow effect in CSS without relying too heavily on hacks or workarounds?

I am seeking to create a rectangular DIV with drop shadows on all four sides. Some suggested solutions involve using negative and positive values for drop shadows or nesting multiple DIVs with different border colors. However, these methods seem makeshif ...

No requests (GET or POST) data being received by jQuery DataTables when using AJAX for server side processing

I am currently working on implementing a basic DataTables feature that I want to update whenever I apply search or filters to any column (or the "entire table" option at the top right corner of the page). Clicking on a column header to sort triggers a requ ...

The arrangement of columns and floating images along with the surrounding white spaces

There are 3 columns, each sized at 33% with images of varying heights inside. When viewed on a device the size of an iPad, I want to change the column sizes to be 50%. However, when I do this, the third column appears in the middle bottom of the top two co ...

What is the best way to create distance between two rows?

I am trying to create space between two rows in an HTML file Where should I insert the code to add space between the two rows? <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.js"></script> <link rel="s ...

Unable to Scroll HTML Division

At the moment, I am working on a new website. I have a div where I place images and overlay text on them to describe the images. The issue I am facing is that the text does not scroll even though the scroll bar is visible. This is my HTML: <!DOCTY ...

What is the process for inserting a generated value into a graph?

Hello there! I'm new to the world of web design and might not have all the technical terms down just yet. My current project involves creating a graph that displays the average rating for each lecture. I've already calculated the averages for all ...