Button click to fade in and out content with jQuery

Example of button click to fade in and out content with toggle state using jQuery.

button click to show and hide contentCodePen example.

Firstly a couple of functions:

The fade out and remove the element function:

function fadeRemove(element, length) {
  $(element).fadeOut(length, function () {

Check if element exists and remove it:

function alreadyExists(element) {
  if ($(element).length) {
    fadeRemove(element, 400);

The main Javascript part, monitor the button clicks and react based on their state:

var btnOneClicked = false;
var btnTwoClicked = false;

$(document).ready(function () {
  $("#btnOne").click(function () {
    if (btnOneClicked) {
      fadeRemove("#btnOneCard", 400);
      btnOneClicked = false;
    } else {
      btnOneClicked = true;
      var cardOne =
        '<div class="col-12"><div class="card text-white bg-primary" id="btnOneCard"><div class="card-header">ONE</div><div class="card-body"><h4 class="card-title">btnOne</h4></div></div>';
  $("#btnTwo").click(function () {
    if (btnTwoClicked) {
      fadeRemove("#btnTwoCard", 400);
      btnTwoClicked = false;
    } else {
      var cardTwo =
        '<div class="col-12"><div class="card text-white bg-success" id="btnTwoCard"><div class="card-header">TWO</div><div class="card-body"><h4 class="card-title">btnTwo</h4></div></div></div>';
      btnTwoClicked = true;

The two buttons start as normal (not actively clicked), once clicked a card will fade in and the button will obtain the active class. If that button is clicked again then the card fades out and the button returns to normal state i.e the active class gets removed.

Basic HTML layout:

<div class="container">
  <div class="row text-center mt-3" id="buttons">
    <div class="col-12 mb-3">
      <a href="#" id="btnOne" class="btn btn-primary mx-2" role="button">btn One</a>
      <a href="#" id="btnTwo" class="btn btn-success mx-2" role="button">btn Two</a>

Video example: