ted_chou12 Posted January 30, 2007 Share Posted January 30, 2007 I am just looking for how to hide and show html parts, something like this:[code]function show_hide(the_layer){ if(document.getElementById(the_layer)) { if(document.getElementById(the_layer).style.display == 'none') { document.getElementById(the_layer).style.display = 'inline'; } else { document.getElementById(the_layer).style.display = 'none'; } }}[/code][code]<div id="foobar">Some text to be hidden or displayed.</div><br /><a href="javascript:show_hide('foobar');">Show/hide</a>[/code]However, instead of hide at the first place, this codes shows it first and hide after you press the link, i tried reversing it, but it doesnt work, can anyone help me with this?ThanksTed Quote Link to comment Share on other sites More sharing options...
tomfmason Posted January 30, 2007 Share Posted January 30, 2007 Well first you should understand that if you use display, the div will not take any space on the page until the display is set to block. You can also use visibility. Here is a simple example. Also, I see that you repeated document.getElementById(The_layer); several times.. Just do something like this next time.[code]function showHideDiv(id) { var div = document.getElementById(id); div.style.display = (div.style.display == 'block')? 'none' : 'block';}[/code]As I said you can also use visibility. Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Author Share Posted February 1, 2007 nope. I cant get it to work, can you suggest me how should i put this?ThanksTed Quote Link to comment Share on other sites More sharing options...
fenway Posted February 1, 2007 Share Posted February 1, 2007 It's hard not to get a 3-line function to work... how do you mean? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 2, 2007 Author Share Posted February 2, 2007 I dont know where to put my html in, i am not very experienced with javascript...Ted Quote Link to comment Share on other sites More sharing options...
janroald Posted February 2, 2007 Share Posted February 2, 2007 html[code]<div id="foobar" style="display:block;">Some text to be hidden or displayed.</div><br /><a href="javascript:show_hide('foobar','block');">Show/hide</a>[/code]js[code]function showHideDiv(id,style) { var element = document.getElementById(id); element.style.display = (element.style.display == style ? 'none' : 'block');}[/code]This should cover your needs and get you started.Remember to give the div a display:block or display:inline or else you will have an extra click to make things happen. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Author Share Posted February 3, 2007 I see, thanksTed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.