javascript - jQuery click function to change the table position -
i have 3 table in #middlebox click list push table top example, position of table starter -> soup ->seafood. when click #seafood_li seafood table show @ top. table set seafood -> starter -> soup. when click #seafood_li again normal. jquery code function of toggle , hide m not sure how modify it
<div class="main"> <ul class="top_menu" > <li class="orderlist" id="starter_li">starter</li> <li class="orderlist" id="soup_li"> soup</li> <li class="orderlist" id="seafood_li">seafood</li> <li class="orderlist" id="return_li">return all</li> </ul> <div id="middlebox"> <table class="food_table" id="starters" style="width:100%"> <tbody> <tr> <td>s1</td> <td>starter1<br> <td>10</td> </tr> </tbody> </table> <table class="food_table" id="soups" style="width:100%"> <tbody> <tr> <td>1</td> <td>soup1</td> <td>4</td> </tr> </tbody> </table> <table class="food_table" id="seafood" style="width:100%"> <tbody> <tr> <td>2</td> <td>seafood1</td> <td>7</td> </tr> </tbody> </table> </div>
jquery
$(document).ready(function() { $(".orderlist").not("#return_li").on("click", function(e) { var elem = $(this); $("#" + this.id.slice(0, -3) + "s").fadetoggle(500); var color = elem.css('background-color'); console.log(color); if(color === 'rgb(255, 255, 255)') { elem.css("background-color", "#05ff0a"); } else { elem.css("background-color", "rgb(255, 255, 255)"); } }); $("#return_li").click(function() { $(".food_table").fadein(500); $(this).siblings().each(function() { $(this).css("background-color","rgb(255,255,255)"); }); }); });
notice id's i've modified in html,
added bit of css...
see jquery. i've commented interesting parts. should clear it's doing.
by clicking li element this.id.split("table_")[1]
remove "table_" portion string retrieving whole table id:
if i.e: li's id is: table_starters
retrieved is starters
.
i'm not sure what's intention green colors... should started.
$(function() { // dom ready var $orderlist = $(".orderlist"); var $middlebox = $("#middlebox"); var originalhtml = $middlebox.html(); // memorize it! $orderlist.not("#orderreturn").on("click", function(e) { // retrieve second pard of button id // it's relation table var tableid = this.id.split("table_")[1]; // see html > modified ids $(this).toggleclass("active"); // see css!!! $("#"+ tableid).prependto( $middlebox ); }); $("#orderreturn").click(function() { $orderlist.removeclass("active"); // remove special classes $middlebox.html( originalhtml ); // restore original (memorized) html }); });
.food_table{ width:100%; background:#eee; margin:10px 0; } .orderlist.active{ background:#05ff0a; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main"> <ul class="top_menu"> <li class="orderlist" id="table_starters">starter</li> <li class="orderlist" id="table_soups"> soup</li> <li class="orderlist" id="table_seafoods">seafood</li> <li class="orderlist" id="orderreturn">return all</li> </ul> <div id="middlebox"> <table class="food_table" id="starters"> <tr> <td>s1</td> <td>starter1</td> <td>10</td> </tr> </table> <table class="food_table" id="soups"> <tr> <td>1</td> <td>soup1</td> <td>4</td> </tr> </table> <table class="food_table" id="seafoods"> <tr> <td>2</td> <td>seafood1</td> <td>7</td> </tr> </table> </div> <!-- !#middlebox --> </div> <!-- !#main -->