Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help on Cookie-based Toggle Menu
    text
    copied!<p>I've written a cookie-based toggle show/hide menu. It remembers what was open/closed using the jQuery cookie plugin. It exactly works fine; the only problem is I have to add different script for each menu. So each menu has its own script.</p> <p>This is the code so far:</p> <p>jQuery</p> <pre><code>$(document).ready(function() { var panel = $("#f1_fold"); var button = $("#f1_fold_i"); var initialState = "expanded"; var activeClass = "hidden"; var visibleText = "&lt;img src='html/sys-img/min.gif' alt='-' /&gt;"; var hiddenText = "&lt;img src='html/sys-img/plus.gif' alt='+' /&gt;"; if($.cookie("panelState1") == undefined) { $.cookie("panelState1", initialState); } var state = $.cookie("panelState1"); if(state == "collapsed") { panel.hide(); button.html(hiddenText); button.addClass(activeClass); } button.click(function(){ if($.cookie("panelState1") == "expanded") { $.cookie("panelState1", "collapsed"); button.html(hiddenText); button.addClass(activeClass); } else { $.cookie("panelState1", "expanded"); button.html(visibleText); button.removeClass(activeClass); } panel.slideToggle("1000"); return false; }); }); </code></pre> <p>And here is the HTML snippet</p> <pre><code>&lt;div class="tableborder"&gt; &lt;div class='maintitle'&gt; &lt;div style='float: right; width: auto !important; padding-right: 6px; padding-left: 3px;'&gt;&lt;a id='f1_fold_i' id='1' href='#' style='vertical-align:middle; border:0px solid #000; cursor:pointer;' /&gt;&lt;img src='html/sys-img/min.gif' alt='-' /&gt;&lt;/a&gt;&lt;/div&gt; &lt;div&gt;&lt;img src='style_images/1/nav_m.gif' border='0' alt='&amp;gt;' width='8' height='8' /&gt;&amp;nbsp;&lt;a href="index.php?c=1"&gt;Gotei 13&lt;/a&gt;&lt;/div&gt; &lt;/div&gt; &lt;span id='f1_fold' style='display:;'&gt; &lt;table width="100%" border="0" cellspacing="1" cellpadding="4"&gt; &lt;tr&gt; &lt;th align="left" colspan="2" width="59%" class='titlemedium'&gt;Forum&lt;/th&gt; &lt;th align="right" width="14%" class='titlemedium'&gt;Stats&lt;/th&gt; &lt;th align="left" width="25%" class='titlemedium'&gt;Last Post Info&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="row4" align="center"&gt;&lt;img src='subnonew.gif' border='0' alt='No New Posts' /&gt;&lt;/td&gt; &lt;td class="row4"&gt;&lt;b class="dtitle"&gt;&lt;a href="index.php?showforum=1"&gt;Rules &amp;amp; Announcement Board&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;&lt;span class="desc"&gt;Pengumuman, panduan, dan peraturan forum dipajang di sini. Baru daftar? Yuk tengok dulu sebelum posting~&lt;br /&gt;&lt;/span&gt;&lt;/td&gt; &lt;td class="row2" align="right"&gt;58 Topics &lt;br /&gt;1410 Replies&lt;/td&gt; &lt;td class="row2" nowrap="nowrap"&gt; &lt;a href='index.php?showtopic=2069&amp;amp;view=getlastpost' title='Go to last post'&gt;&lt;img src='http://209.85.62.23/style_images/1/lastpost.gif' border='0' alt='Last Post' /&gt;&lt;/a&gt; Today at 01:59 am&lt;br /&gt;In:&amp;nbsp;&lt;a href='index.php?showtopic=2069&amp;amp;view=getnewpost' title='[INFO] Gelar Jepang UI 2011 (Go to first unread)'&gt;[INFO] Gelar Jepang UI 2011&lt;/a&gt;&lt;br /&gt;By: &lt;a href='index.php?showuser=1'&gt;Xaliber&lt;/a&gt; &lt;br /&gt;Thread: &lt;a href='index.php?showforum=36'&gt;Gathering &amp;amp; Events&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;&lt;/span&gt;&lt;table width="100%"&gt;&lt;tr&gt; &lt;td class='darkrow2' colspan="5"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>Notice that I have to add different script for each menu because it depends on the <code>f1_fold</code> and <code>f1_fold_i</code> css ID. For the other menu, it would be <code>f2_fold</code> and <code>f2_fold_i</code>. For the other, it'd be <code>f3_fold</code> and <code>f3_fold_i</code>. Etc.</p> <p>Also, to make the cookie remembers which menu is collapsed and which menu is opened, it also depends on the <code>panelState1</code> (for 1st menu), <code>panelState2</code> (for the 2nd), <code>panelState3</code>, etc</p> <p>Is there any way to make this shorter and more flexible? So I don't have to add different variable</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload