Multi Spoilers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Misan
    New Member
    • Jun 2011
    • 1

    Multi Spoilers

    Hi,
    I am really new to javascript so dont blame me please.

    I tried to perform a Spoiler, which went actually pretty good:

    Code:
    <script type="text/javascript">
    function spoil(id){
    	if (document.getElementById) {
    		var divid = document.getElementById(id);
    		divid.style.display = (divid.style.display=='block'?'none':'block');
    	} 
    }
    </script>
    
    
            <a  onmouseover="document.0.src='chars/guy_hover.png'" onmouseout="document.0.src='chars/guy.png'" >
            <img style="cursor:pointer;" onclick="spoil('id0')" src="chars/guy.png" name="0">
    
            </a>
    This is yet very undynamic.
    However, I tried to add extra functionality: close all other spoilers when opening one.
    I tried with a while which should close all spoilers, before opening a new one.
    I changed the script to this, using a fixed value for the while because I have no idea how to count document.getele mentbyid elements :S

    Code:
    <script type="text/javascript">
    function spoil(id){
    
        if (document.getElementById) {
        var divid = document.getElementById(id);
        var i=0;
        while (i<=300)
        {
        i.style.display = (i.style.display=='none');
         i++;
        }
        divid.style.display = (divid.style.display=='block'?'none':'block');
    	} 
    }
    </script>
    And at this point, I have no more access to my spoilers, they wont open at all :/
    Since javascript offers no error code I can give none, sorry.

    Please help me.
    Thanks in advance
    - Misan
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I have no idea how to count document.getele mentbyid elements :S
    document.getEle mentById() always returns 1 Element or null if there is no element with that id.

    additionally, Numbers (i) do not have a style property.

    Comment

    Working...