Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Background color of Active list item in bootstrap

I have item group list

<div id="MainMenu">
    <div class="list-group panel">
        <a href="#why" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu">Menu 1</a>
        <div class="collapse" id="why">
            <a href="" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1">Menu 1 a</a>
            <a href="" class="list-group-item">Menu 1 b</a>
            <a href="" class="list-group-item">Menu 1 c</a>
            <a href="" class="list-group-item">Menu 1 d</a>
            <a href="" class="list-group-item">Menu 1 e</a>
            <a href="" class="list-group-item">Menu 1 f</a>
        </div>
        <a href="#joinus" class="list-group-item" data-toggle="collapse" data-parent="#MainMenu">Menu 2</a>
        <div class="collapse" id="joinus">
            <a href="" class="list-group-item">Menu 2 a</a>
            <a href="" class="list-group-item">Menu 2 b</a>
            <a href="" class="list-group-item">Menu 2 c</a>
            <a href="" class="list-group-item">Menu 2 d</a>
            <a href="" class="list-group-item">Menu 2 e</a>
        </div>
    </div>
</div>

I want to change background of active list item, I Know how to change background, but I am unable to get which list is active, or inactive by JavaScript, tried lots of solution given on others but didn't woJrk.

JsFiddle

UPDATE:

like image 855
Muhammad Taqi Avatar asked Apr 21 '15 08:04

Muhammad Taqi


People also ask

How can I change bootstrap active link color?

(a:active) changes the color when you click on the link. You can test this by holding down the mouse button on the link that you made a different color with the (a:active). Finally, if you are trying to do this using just CSS you have to add a specific class on the current link that the end user is on.

How do I change the active color in CSS?

Use the :active class to change the color of active links. Possible values could be any color name in any valid format.

How do I change the background color of a list in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.


1 Answers

What i does it assign and id to every link in list that is also the page name, and made a js function that is called on body load of the page. the function get the current file name from url and determines which page is this, then by js i made that link class active. this solve my problem. however i share this solution for others to improvement.

function get_current_page() {
  var pathArray = window.location.pathname.split("/");
  var current_page = pathArray[pathArray.length - 1];
  current_page_array = current_page.split(".");
  current_page = current_page_array[0];

  if (
    current_page == "students" ||
    current_page == "my-profile" ||
    current_page == "faqs" ||
    current_page == "forecast-career"
  ) {
    document.getElementById("joinuslist").className += " active";
    document.getElementById("joinus").className += " in";

    if (current_page == "students") {
      document.getElementById("students").className += " active";
    } else if (current_page == "faqs") {
      document.getElementById("faqs").className += " active";
    } else if (current_page == "forecast-career") {
      document.getElementById("forecast-career").className += " active";
    } else if (current_page == "my-profile") {
      document.getElementById("my-profile").className += " active";
    } else {
    }
  } else if (
    current_page == "values" ||
    current_page == "ambassadors" ||
    current_page == "documentary"
  ) {
    if (current_page == "values") {
      document.getElementById("values").className += " active";
    } else if (current_page == "ambassadors") {
      document.getElementById("ambassadors").className += " active";
    } else if (current_page == "documentary") {
      document.getElementById("documentary").className += " active";
    } else {
    }
  }
}
.list-group-item.active:hover {
  background-color: #aed248 !important;
  border-color: #aed248 !important;
}
.list-group-item.active,
.list-group-item.active:hover {
  background-color: #007715 !important;
  border-color: #aed248 !important;
}

#joinus .list-group-item.active,
.list-group-item.active:hover {
  background-color: #adce1b !important;
  border-color: #adce1b !important;
}
#whyptcl .list-group-item.active,
.list-group-item.active:hover {
  background-color: #adce1b !important;
  border-color: #adce1b !important;
}
.panel {
  margin-bottom: 20px;
  background-color: transparent !important;
  border: 0px solid transparent;
  border-radius: 5px;
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
}
<body onload="get_current_page()">
  <div id="MainMenu">
    <div class="list-group panel">
      <a
        id="whylist"
        href="#why"
        class="list-group-item"
        data-toggle="collapse"
        data-parent="#MainMenu"
        >Menu 1</a
      >
      <div class="collapse" id="why">
        <a
          id="values"
          href="values.html"
          onclick="activate(this)"
          class="list-group-item"
          data-toggle="collapse"
          data-parent="#SubMenu1"
          >Menu 1 a</a
        >
        <a
          id="ambassadors"
          href="ambassadors.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 1 b</a
        >
        <a
          id="documentary"
          href="documentary.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 1 c</a
        >
      </div>
      <a
        id="joinuslist"
        href="#joinus"
        class="list-group-item"
        data-toggle="collapse"
        data-parent="#MainMenu"
        >Menu 2</a
      >
      <div class="collapse" id="joinus">
        <a
          id="my-profile"
          href="my-profile.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 2 a</a
        >
        <a
          id="students"
          href="students.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 2 b</a
        >
        <a
          id="forecast-career"
          href="forecast-career.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 2 c</a
        >
        <a
          id="faqs"
          href="faqs.html"
          onclick="activate(this)"
          class="list-group-item"
          >Menu 2 e</a
        >
      </div>
    </div>
  </div>
</body>
like image 194
Muhammad Taqi Avatar answered Oct 19 '22 12:10

Muhammad Taqi