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

189
Views
Javascript to change button text back and forth using bootstrap 4 & fontawesome icons

I'm using bootstrap 4 to create a button with a dropdown menu. The functionality I need is that the button when closed contains the word open and a fontawesome icon beside it. When opened it should say close with an icon beside it.

I'm currently trying this

function toggle() {
  const btn = document.body.querySelector("#dropdownMenu2");

  if (btn.innerText == "Open") {
    btn.innerHTML = "<i class='fa fa-plus-circle' aria-hidden='true'></i>" + "Open";
  } else {
    btn.innerHTML = "<i class='fa fa-minus-circle' aria-hidden='true'></i>" + "Close";
  }
}
.dropdown-toggle {
  border-radius: 0;
  border-style: solid;
  border-color: #9d2235;
  background-color: #9d2235;
  color: #fff;
  padding: 8px 20px;
}
<button class="dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="toggle()">
               <i class="fa fa-plus-circle" aria-hidden="true"></i>                
                Actions
</button>

The button changes to close on the first click but never changes back to open when clicked again.

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

0

instead of trying to alter the dom. Why dont you just add a second button with exact details and then just add same class on both the buttons and use jquery(since bootstrap 4 uses jquery) to hide and display other like following

$('.buttonClass').toggle();
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!