3

The following works to replace a div with a new div...

<div id = "div1" style="display:block" onclick = "replace()"><img src="1.jpg" /></div>

<div id = "div2" style="display:none"><img src="2.jpg" /></div>

<script type = "text/javascript">
function replace() {
document.getElementById("div1").style.display="none";
document.getElementById("div2").style.display="block";
}

</script>

What I can't figure out is how to make this work so when you click div2 it is replaced by div3 and so on.

In other words, I want to replace the div on each click more than just once. What's the best way to go about this? I'm a novice, so not sure if the above is a good start or not.

Thanks!

1
  • 1
    What do you mean replace? You want, whenever you click on a div, to hide that div and show the next one after it? Commented May 21, 2012 at 14:48

2 Answers 2

4

You could make a more generic function:

function replace( hide, show ) {
  document.getElementById(hide).style.display="none";
  document.getElementById(show).style.display="block";
}

Then you can create many divs and use the same function:

<div id = "div1" style="display:block" onclick = "replace('div1','div2')">...</div>
<div id = "div2" style="display:none" onclick = "replace('div2','div3')">..</div>
<div id = "div3" style="display:none" onclick = "replace('div3','div4')">..</div>
...
Sign up to request clarification or add additional context in comments.

1 Comment

You could also swap the first variable with 'this': replace(this,'div2')...etc.
3

I will suggest you some best practices in this answer:

  • Use classes instead of the style property, it's way nicer for the browser.
  • Don't use inline event handler. See the example below.
  • It's not "replace" you're looking for, it's "toggling".
  • I suggest you use event bubbling. This way, you add a single event on the container of all your div, and you can work on this.

Alright, now for the example:

HTML:

<div id="container">
    <div id="div1">..</div>
    <div id="div2" class="hidden">..</div>
    <div id="div3" class="hidden">..</div>
</div>

JS:

// Notice how I declare an onclick event in the javascript code
document.getElementById( 'container' ).onclick = function( e ) {

    // First, get the clicked element
    // We have to add these lines because IE is bad.
    // If you don't work with legacy browsers, the following is enough:
    //     var target = e.target;
    var evt = e || window.event,
        target = evt.target || evt.srcElement;

    // Then, check if the target is what we want clicked
    // For example, we don't want to bother about inner tags
    // of the "div1, div2" etc.
    if ( target.id.substr( 0, 3 ) === 'div' ) {

        // Hide the clicked element
        target.className = 'hidden';

        // Now you have two ways to do what you want:
        //     - Either you don't care about browser compatibility and you use
        //       nextElementSibling to show the next element
        //     - Or you care, so to work around this, you can "guess" the next
        //       element's id, since it remains consistent
        // Here are the two ways:

        // First way
        target.nextElementSibling.className = '';

        // Second way
        // Strip off the number of the id (starting at index 3)
        var nextElementId = 'div' + target.id.substr( 3 );
        document.getElementById( nextElementId ).className = '';
    }
};

And of course, the CSS:

.hidden {
    display: none;
}

I highly suggest you read the comments in the javascript code.

If you read carefully, you'll see that in modern browsers, the JS code is a matter of 5 lines. No more. To support legacy browsers, it requires 7 lines.

7 Comments

Thanks for the pointers. I tried this method in FF and Chrome (trying your first and then your second way), but after the first click the div becomes hidden and doesn't display the next div.
I guess you've done something wrong, then? Here is a jsfiddle showing the code working: jsfiddle.net/Ralt/EPKwT
Very strange... I must be missing something. Works fine in jsfiddle, but when I copy/paste the code you have in jsfiddle into a blank html document to test, it doesn't work on any browser. Just shows "1" and clicking the div doesn't change anything ("1" remains).
Wild guess: put the script tag right before the closing body tag.
Yep - that did it. Also when I copied and pasted it entered a couple weird hidden characters I found with Firebug. The script works, but when I add images to the div, it breaks. For example... <div id="div2" class="hidden"><img src="images/x.jpg" width="600" height="400" border="0" /></div>
|

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.