tags:

views:

40

answers:

4

I have a dynamic page which has a parent div called 'divWiz' where I have to do the following:

  • count total div's inside 'divWiz' and hide 50% of them. For example: If I have 50 child div's inside divwiz, I want to show 25 and hide the rest.

  • At the end of the 25 div's, two links should appear that says 'Show Next' and 'Show All'.

If the user clicks the 'Show Next' link, the first 25 should be hidden and the next 25 visible. 'Show Next' link should be invisible now.

If the user clicks 'Show All', all the div's should be visible. Both the links will disappear once the user clicks Show All.

How can I do this? Since this involves lot of traversing, I am looking for jquery code with performance.

+2  A: 

Here are some hints to get you started:

  • $('#divWiz > div') will get you all the DIVs within "divWiz".
  • $(something).length is the number of selected elements.
  • $(something).slice(start, end) lets you operate on a subset of selected elements.

You should be able to put these together to do what you want.

casablanca
+1  A: 

all you need to do is have onclick handlers that show/hide either firstHalf, secondHalf or both.

var divCount   = $('#divWiz>div').length;
var firstHalf  = $('#divWiz>div').slice(0,divCount/2);
var secondHalf = $('#divWiz>div').slice(divCount/2);
tobyodavies
+1  A: 

jQuery wont give you more performance, it will give you an easier way to implement. If you need better performance, wrap those "packs" of 25 div's into common parents and show/hide those parents(should be 25 times faster)

Dr.Molle
those div's are random..it could be 50, 70 or even 200. So i cannot create packs beforehand
jqlearner
Nothing is really random. I've never seen flying something around on a webpage where nobody knows where it comes from and what it will be. Someone has to create the content(div's), let him "pack" them.
Dr.Molle
You may be right in most cases, but in this case, I am pretty sure that the number of child div's inside divwiz is not known beforehand. That's why i mentioned in my post that it is dynamic page.
jqlearner
If it is dynamic, why can't the server side code pack it for you?
tobyodavies
A: 

You can make use of the index numbers:

$('#divWiz > div:lt(25)')          // all divs of index 0 to 24

$('#divWiz > div:lt(50):gt(24)')  // all divs of index 25 to 49

Caution! :gt(-1) doesn't work!

So, here's a quick and dirty full working example of something like what you're describing:

HTML:

<input value=" Next " type="button"/>
<input value=" Prev " type="button"/>
<input value=" Show All " type="button"/>
<div id="divWiz"></div>

JS:

$(function() {
    var i, cutoff, total = 50;
    for(i=0; i<total; ++i)
        $(document.createElement("div")).html(i).appendTo("#divWiz");

    cutoff = total/2;

    $("#divWiz > div").hide();
    $("#divWiz > div:lt("+cutoff+")").show();

    $("input[value=' Next ']").click(function() {       
        $("#divWiz > div").hide();        
        $("#divWiz > div:gt("+(cutoff - 1)+")").show();
    });

    $("input[value=' Prev ']").click(function() {       
        $("#divWiz > div").hide();        
        $("#divWiz > div:lt("+cutoff+")").show();    });    

    $("input[value=' Show All ']").click(function() {       
        $("#divWiz > div").show();        
    });        
});

Try it out with this jsFiddle

Peter Ajtai
Peter thank you so much for sparing the time. I do not want all the links to be shown at the same time. They should be visible on the conditions as listed in my post. Can that be done? Moreover I do not if the child div's will be 50, 100 or 200. So how can i avoid hardcoding? thanks
jqlearner
@jqlearner - Oh misunderstood a little. Just calculate `up` and `low` using `$(divWiz > div).length`
Peter Ajtai
Ok that will help. you said this is a quick and dirty way. Does that mean it has to be tuned further for performance? thanks peter
jqlearner
@jqlearner - edited the answer to show either first or second half of the divs. ----- I think you might be able to speed it up a little. For example if you use `$("#someId").click(...)` for each click instead of an element selector with a pseudo selector you might get quicker. Also if you cache the two sets of divs (store them in a variable)....... You'd have to test it to see if it makes a noticeable difference with the numbers you're using.
Peter Ajtai