function grid_view() {
grid_view_result();
grid_view_button();
}
function grid_view_active() {
if (document.getElementById("grid_view").className == "fa fa-th-large m-l-5") {
document.getElementById("grid_view").className = "fa fa-th-large m-l-5 active";
}
}
function grid_view_inactive() {
if (document.getElementById("grid_view").className == "fa fa-th-large m-l-5 active") {
document.getElementById("grid_view").className = "fa fa-th-large m-l-5";
}
}
function grid_view_button() {
if (document.getElementById("cooking_result").className == "recipes grid") {
grid_view_active();
return;
}
if (document.getElementById("cooking_result").className = "recipes list") {
grid_view_inactive();
return;
}
}
function grid_view_result() {
if (document.getElementById("cooking_result").className == "recipes list") {
document.getElementById("cooking_result").className = "recipes grid";
}
}
function list_view() {
list_view_result();
list_view_button();
}
function list_view_active() {
if (document.getElementById("list_view").className == "fa fa-th-list m-l-5") {
document.getElementById("list_view").className = "fa fa-th-list m-l-5 active";
}
}
function list_view_inactive() {
if (document.getElementById("list_view").className == "fa fa-th-list m-l-5 active") {
document.getElementById("list_view").className = "fa fa-th-list m-l-5";
}
}
function list_view_button() {
if (document.getElementById("cooking_result").className == "recipes list") {
list_view_active();
return;
}
if (document.getElementById("cooking_result").className = "recipes grid") {
list_view_inactive();
return;
}
}
function list_view_result() {
if (document.getElementById("cooking_result").className == "recipes grid") {
document.getElementById("cooking_result").className = "recipes list";
}
}
.active{text-decoration:underline;}
.recipes.grid li{float:left;width:50%;height:50px;background:red;}
.recipes li:nth-child(2){background:blue !important;}
.recipes.list li{float:left;width:100%;height:50px;background:red;}
<i id="grid_view" onclick="grid_view();" class="fa fa-th-large m-l-5">Grid</i>
<i id="list_view" onclick="list_view();" class="fa fa-th-list m-l-5">List</i>
<ul id="cooking_result" class="recipes grid">
<li></li>
<li></li>
</ul>
I can add class="active" to i tag but I can't remove when I click to other i tag. May you help me to fix it? Also, If possible, may you simplify to javascript? I think it's too long for this functionality :)