Solved

javascript onClick change div

Posted on 2004-08-19
4
2,228 Views
Last Modified: 2007-12-19
Hello,

I have two links (images), first for backward second for forward.

I would linke to change (n) divs back and forward with this two buttons. Example on http://www.otteybypamp.com/en/majcka.asp?menuid=majcka, but there I change only pictures, now I need the layers.

Thank you,
Mitja
0
Comment
Question by:msmid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
johanmulder earned 125 total points
ID: 11840693

<SCRIPT LANGUAGE="JavaScript">

var DivCount = 3;
var DivCurrent = 1;

function showAnotherDiv(forward)
{
    //first, hide current layer
    document.getElementById("imgDiv"+DivCurrent).style.display = "none";

   
    //then, check forward/backward
    if(forward)
    {
       DivCurrent++;      
       if(DivCurrent > DivCount)
          DivCurrent = 1;
     }
     else
     {
        DivCurrent--;
        if(DivCurrent < 1)
           DivCurrent = DivCount;
     }

     //display new layer
     document.getElementById("ïmgDiv"+DivCurrent).style.display = "block";

}

</SCRIPT>


<DIV ID="imgDiv1" STYLE="display:block"><IMG SRC="image1.jpg"></DIV>
<DIV ID="imgDiv2" STYLE="display:none"><IMG SRC="image2.jpg"></DIV>
<DIV ID="imgDiv3" STYLE="display:none"><IMG SRC="image3.jpg"></DIV>

<IMG SRC="backward.jpg" onClick="javascript:showAnotherDiv(false);">
<IMG SRC="forward.jpg" onClick="javascript:showAnotherDiv(true);">

Good luck with this one!
0
 
LVL 5

Expert Comment

by:LindzK
ID: 11840810
When i tested that code in IE6, it didn't work, it does work in ns7 though.

a slight modification to the code,  should make it work in both popular browsers ( as follows : )#

The script takes in the div id, as the one to show, if you need more than two divs, you would just 'hide' more of them, in the javascript block, then make visible the one past into the equation

<script language="javascript">
  var visdiv = 'first';
  function SwapDiv(div)
  {
    if (div != visdiv)
      {
      document.getElementById(div).style.visibility='visible';
        document.getElementById(visdiv).style.visibility='hidden';
        visdiv = div;      
      }
  }
</script>

<div id="second" style="position:absolute; top:0px; left:0px; visibility:hidden;">
second div
</div>
<img src="back.gif" width="32" height="32" alt="" border="0" onclick="SwapDiv('first')">
<img src="forward.gif" width="32" height="32" alt="" border="0" onclick="SwapDiv('second')">
0
 
LVL 1

Expert Comment

by:johanmulder
ID: 11840862
you can also replace those rules:
   document.getElementById("imgDiv"+DivCurrent).style.display = "none";
and  
   document.getElementById("imgDiv"+DivCurrent).style.display = "block";

with:

if(document.all)
   document.all("imgDiv"+DivCurrent).style.display = "none";
else
   document.getElementById("imgDiv"+DivCurrent).style.display = "none";

and

if(document.all)
   document.all("imgDiv"+DivCurrent).style.display = "block";
else
   document.getElementById("imgDiv"+DivCurrent).style.display = "block";

0
 

Author Comment

by:msmid
ID: 11849668
Thank you johanmulder.

Mitja
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Go Fund Me" type plugin 5 65
WordPress 8 69
Syntax error creating JSON recordset 4 43
Wordpress contact form sending spam 2 51
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question