<b>The more button dynamically grabbing extra items:</b>
<div class="width-constraint">
The red box is where the menu would be
<ul class="datalist">
<li>Alpha</li>
<li>Beta</li>
<li>Gamma Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
</ul>
this is just extra text; the orange box is the site container, where content can go above and
below the button bar
</div>
<br />
<hr />
<b>The more button statically placed; or, effectively after jQuery manipulates the DOM:</b>
<ul>
<li>Alpha</li>
<li>Beta</li>
<li>Gamma Delta</li>
<li>Echo</li>
<li class="morebutton">
<a href='#'>More</a>
<ul>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
</ul>
</li>
</ul>
<br style="clear: both;" />
<br />
<hr />
<b>This is a test when the more button is not needed:</b>
<div class="width-constraint">
<ul class="datalist">
<li>Alpha</li>
<li>Beta</li>
<li>Gamma Delta</li>
<li>Echo</li>
</ul>
</div>
// The more button click handler
var fnMoreButtonClick = function(evt) {
evt.preventDefault();
$(this).parent().toggleClass("open");
};
// This is used if the more button is placed statically
$('.morebutton > a').click(fnMoreButtonClick);
// For dynamically grabbing extra (clipped) items)
$(".datalist").each(function() {
var $this = $(this);
// Figure out the last item being displayed
var parentHeight = $this.height();
var lastVisibleItem = $this.children("li")
.filter(function() { return $(this).position().top < parentHeight; })
.last();
debugger;
// Only add the more button if there are hidden items
if(lastVisibleItem.next().length) {
// Add the more button to the list
var liMore = $("<li class='morebutton'><a href='#'>More</a></li>")
.find('a').click(fnMoreButtonClick).end()
.insertAfter(lastVisibleItem);
// Graduate the more button until it is displayed
while(liMore.position().top >= parentHeight) {
var prevLi = liMore.prev();
liMore.detach().insertBefore(prevLi);
}
// Take all the subsequent items and put them "inside" the more button
liMore.nextAll().detach().appendTo($("<ul />").appendTo(liMore));
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.