0

My current html looks like this:

<div class="wrapper" style="display:block;" id="item0"><!--- Stuff --></div>
<div class="wrapper" style="display:none;" id="item1"><!--- Stuff --></div>
<div class="wrapper" style="display:none;" id="item2"><!--- Stuff --></div>
<div class="wrapper" style="display:none;" id="item3"><!--- Stuff --></div>
<div class="wrapper" style="display:none;" id="item4"><!--- Stuff --></div>

And then I have a back/forward link which call the corresponding javascript function, it looks like this:

 function switchNextItem() {  
    var element0 = document.getElementById('item0');
    var element1 = document.getElementById('item1');
    var element2 = document.getElementById('item2');
    var element3 = document.getElementById('item3');
    var element4 = document.getElementById('item4');

    if (element0.style.display == 'block')  {
        element0.style.display = 'none';  
        element1.style.display = 'block';
    } else if (element1.style.display == 'block') {
        element1.style.display = 'none';
        element2.style.display = 'block';
    } else if (element2.style.display == 'block') {
        element2.style.display = 'none';
        element3.style.display = 'block';
    } else if (element3.style.display == 'block') {
        element3.style.display = 'none';
        element4.style.display = 'block';
    } else if (element4.style.display == 'block') {
        element4.style.display = 'none';
        element0.style.display = 'block';
    }
} 

Are there any possibilities for me to make this more dynamically?

Thanks! (I'm not familiar with javascript).

1
  • Would you be interested in diving into the simple world of jQuery? Commented Jul 11, 2011 at 17:50

2 Answers 2

3

You'd love jQuery: http://jsfiddle.net/TCQum/.

function switchNextItem() {
    $('div.wrapper:visible')  .hide()    .next()      .show();
//     select visible div   | hide it | goto next | show that one
}

Edit: A more precise one:

function switchNextItem() {
    var next = $('div.wrapper:visible:eq(0)')
               .hide()
               .next('div.wrapper'); // next one will be stored in next

    if(next.length === 0) { // if there is no next one, show first one agan
        $('div.wrapper:eq(0)').show();
    } else { // otherwise show next one
        next.show();
    }
}

And with .prev:

function switchNextItem() {
    var prev = $('div.wrapper:visible:eq(0)')
               .hide()
               .prev('div.wrapper'); // prev one will be stored in prev

    if(prev.length === 0) { // if there is no prev one, show last one
        $('div.wrapper:last').show();
    } else { // otherwise show prev one
        prev.show();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Gah! I've been on the Java, C# and C++ sides too much! Thank you, I'll check this out.
This is not equivalent if more than one is block (may not be possible). You also have to handle the wrap around of the last case there, next won't wrap to the first item.
You two are the best. Both solutions are working. However, pimvdb, I'm wondering if there is any $('div.wrapper:visible') .hide().previous().show(); way to go where I have a back button?
@Julian Assange: It's called .prev :)
@Julian Assange: Quite the same, but instead of :eq(0), use :last. (And, instead of :eq(0) you could also use :first if that clears things up.)
|
3

Something like this should work.

function switchNextItem() {  
    var items=['item0','item1','item2','item3','item4'];

    for(var i=0;i<items.length;i++){
        var element = document.getElementById(items[i]);

        if (element.style.display == 'block')  {
            element.style.display = 'none';  

            var nextElement = document.getElementById(items[(i+1) % items.length]);
            nextElement.style.display = 'block';

            break;
        }
    }
} 

3 Comments

+1 for actually moving to the first at the end again, I forgot about that. However, you'd need (i+1) % items.length though.
Thanks both of you for two good answers. I will try this out. :)
Good point, updated. As I was typing it I though of that but forgot to check if the parentheses were needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.