javascript - How can I hide elements in my list and add a 'show more' feature? -


i'm using javascript build list of results. have for-loop iterates on data , creates mydata div, , adds results div. let's pretend looks this:

<div id="results">     <div class="mydata">data 1</div>     <div class="mydata">data 2</div>     ...     <div class="mydata">data 20</div> </div> 

what want only display 5 results @ time, , should user wish see more, can click show next 5 or show more button (or similar). ideas?

just clarify, every time user clicks "show more" want 'unhide' next 5 elements, not remaining elements. each click reveals more elements until displayed.

you can use gt() , lt() selectors along :visible pretty here.

the following show next 5 results on clicking , removes link once items visible.

$('.mydata:gt(4)').hide().last().after(     $('<a />').attr('href','#').text('show more').click(function(){         var = this;         $('.mydata:not(:visible):lt(5)').fadein(function(){          if ($('.mydata:not(:visible)').length == 0) $(a).remove();            }); return false;     }) ); 

working example: http://jsfiddle.net/niklasvh/ntv7d/

regardless of other people suggesting here, not hide elements using css, in js instead, because if user has js disabled , hide elements using css, won't them visible. however, if has js disabled, never hidden, nor button appear etc, has full noscript fallback in place + search engines don't hidden content (but won't know hidden if on dom load).


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -