Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Smooth Accordion / Change FA onclick

I am trying to customize an accordion to change an icon when opening and closing. I can't figure out how to move the FA icon after the text, instead of before.

I'd like the set up to show "Categories (chevron-down)" and then when clicked, "Categories (chevron-up)". Here is what I have so far:

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<h1 onclick="arata_ascunde(this);" class="btn btn-info " id="show_hide_bt">
    <i class="fa fa-chevron-down"></i> Categories
</h1>

<div id="showhide" style="display:none;">
        <ul>
            <li><a href="#">Hello World</a>
            </li>
        </ul>
</div>

JS

    function arata_ascunde(h1) {
        var x = $('#showhide');
        $(h1).find('i').remove();
        if ($(h1).text().trim() == 'Categories') {
            $(h1).html($('<i/>',{class:'fa fa-chevron-up'})).append(' Hide');
            x.fadeIn();
        }
        else {
            $(h1).html($('<i/>',{class:'fa fa-chevron-down'})).append(' Categories');
            x.fadeOut();
        }
    }

CodePen: https://codepen.io/chadwicked123/pen/porRoOq

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to use prepend on js

here is the updated code

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<h1 onclick="arata_ascunde(this);" class="btn btn-info " id="show_hide_bt">
     Categories
  <i class="fa fa-chevron-down"></i>
</h1>

<div id="showhide" style="display:none;">
        <ul>
            <li><a href="#">Hello World</a>
            </li>
        </ul>
</div>

For js

function arata_ascunde(h1) {
    var x = $('#showhide');
    $(h1).find('i').remove();
    if ($(h1).text().trim() == 'Categories') {
        $(h1).html($('<i/>',{class:'fa fa-chevron-up'})).prepend(' Hide');
        x.fadeIn();
    }
    else {
        $(h1).html($('<i/>',{class:'fa fa-chevron-down'})).prepend(' Categories');
        x.fadeOut();
    }
}

Codepen: https://codepen.io/nahid_hridoy/pen/ZEJLEwN

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!