Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JS show/hide div problem

Options
  • 24-08-2005 10:02am
    #1
    Closed Accounts Posts: 425 ✭✭


    I have a small javascript I'm using to hide a text box and show it when a particular option from an option menu is selected

    It's working fine but IE6 is telling me there is a javascript error. I don't really know what's going on (just copied and modified the script). Could anyone here explain what I need to get rid of the error.

    Cheers.



    <script>
    function showhide(obj)
    {
    	var id = obj
    
    	divs = document.getElementsByTagName('div')
    
    
    	for(i =0; i <divs.length; i++)
    	{
    		if(divs[i].id != id)
    		{
    		divs[i].style.visibility = "hidden";
    		}
    	}
    	
    	document.getElementById(id).style.visibility = "visible";
    
    }
    </script>
    
    
    
    <SELECT NAME="type" onkeypress="return enter(document.newci.create)" onchange="showhide(this.options[this.selectedIndex].value);">
    
    <OPTION VALUE="1">1</OPTION>
    <OPTION VALUE="2">2</OPTION>
    <OPTION VALUE="newcategory">New category...</OPTION>
    
    <div id = "newcategory" style = "visibility:hidden">New Category: <input type="text" name="version" size=30></div>
    


Comments

  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    You didn't tell us what error you were getting ...

    IE blocks the content when loading it as a file (rather than over HTTP), so I just Allow Blocked Content, and it's fine (except for the other error below)...

    I was getting an error (JS error, that is) when I chose 1 or 2 from the drop down, because your showhide function was doing stuff to an element that didn't exist. So I just added a check that it existed to get around that, and changed
    document.getElementById(id).style.visibility = "visible";
    
    to
    if( document.getElementById(id) ) document.getElementById(id).style.visibility = "visible";
    
    .


  • Closed Accounts Posts: 425 ✭✭alantc


    Sorry, the error just pointed to a line number and didn't seem to tell me anything.

    That solution worked perfectly in the page I intended it for but I just put it onto another and it makes all my divs hidden except what is selected with the option box! Any ideas how I can only show or hide the one div without it affecting the others?


    Thanks.


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    the block that starts with "for(i =0; i <divs.length; i++)" is what's hiding all the divs.. so you could replace that block with lines to only hide the divs you want, not all of them


Advertisement