Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

JS show/hide div problem

  • 24-08-2005 09: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, Registered Users 2 Posts: 3,890 ✭✭✭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, Registered Users 2 Posts: 3,890 ✭✭✭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