Show-hide function question from newbie

Discussion in 'JavaScript' started by caglarozdag, Jun 30, 2008.

  1. #1
    hi all, i started using JavaScript yesterday so i am really really new. When i call the following functions on a page an empty gap appears bellow the footer. I don't know how to eliminate it.

    Show hide functions.
    function show(elemID)
    {
    	var elem=document.getElementById(elemID);
    	elem.style.position='relative';
    	elem.style.visibility='visible';
    }
    function hide(elemID)
    {
    	var elem=document.getElementById(elemID);
    	elem.style.position='absolute'
    	elem.style.visibility='hidden';
    }
    function show_hide(elemID)
    {
    	var elem=document.getElementById(elemID);
    	if(elem.style.visibility=='hidden')
    	{
    		show(elemID);
    	}
    	else
    	{
    		hide(elemID);
    	}
    }
    Code (markup):
    how i call it in the php code:

    A_PARAGRAPH_REFERRING_TO_AN_IMAGE...<a href='#' onclick='javascript: show_hide(9); return false;'>Click to see the image.</a>
    
    <div style='visibility: hidden; position: absolute;' class='faq' id='9'><img src="./include/hurriyet1_main.jpg"></div>
    Code (markup):
    Thanks already.
     
    caglarozdag, Jun 30, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The code that you use doesn't completely hide the element, it just makes it invisible, but the element is still there and is taking some of the space. I'd recommend changing the show() and hide() to something like that:


    <script type='text/javascript'>
    function show(elemID)
    {
    	var elem=document.getElementById(elemID);
    	elem.style.display='block';
    }
    function hide(elemID)
    {
    	var elem=document.getElementById(elemID);
    	elem.style.display='none'
    }
    function show_hide(elemID)
    {
    	var elem=document.getElementById(elemID);
    	if(elem.style.display=='none')
    	{
    		show(elemID);
    	}
    	else
    	{
    		hide(elemID);
    	}
    }
    </script>
    
    <a href='#' onclick='javascript: show_hide(9); return false;'>Click to see the image.</a>
    
    <div style='position: absolute;' class='faq' id='9'><img src="./include/hurriyet1_main.jpg"></div>
    Code (markup):

    There are some minor changes in the rest of the code too, to make it work with the new show()/hide() functions.


    This will completely hide the element and it will no longer disorder anything on the page
     
    xlcho, Jul 1, 2008 IP
  3. caglarozdag

    caglarozdag Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi,

    thanks a lot for your reply. i've changed the functions and the way i call them like you said. but the gap under the footer is still there and now when i open the page for the first time, the object isn't hidden.
     
    caglarozdag, Jul 1, 2008 IP
  4. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, my bad. I've forgotten to hide the element. Change this:
    <div style='position: absolute;' class='faq' id='9'><img src="./include/hurriyet1_main.jpg"></div>
    Code (markup):
    to

    <div style='display: none; position: absolute;' class='faq' id='9'><img src="./include/hurriyet1_main.jpg"></div>
    Code (markup):
    You can try changing the display property of the element to inline instead of block. Change show() to:
    function show(elemID)
    {
    	var elem=document.getElementById(elemID);
    	elem.style.display='inline';
    }
    Code (markup):
    This will render the element without line breaks before and after it. Depends on your layout, but i think this is the problem that's causing the gap.
     
    xlcho, Jul 1, 2008 IP
  5. caglarozdag

    caglarozdag Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi,

    i've made the changes but the gap is still there. another problem with the modified version that i forgot to mention earlier is that the object now opens up on top of all the content (instead of shifting everything down). Although i actually like it :) serves my purpose much better :) so i'll probably keep it.
     
    caglarozdag, Jul 1, 2008 IP
  6. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #6
    can you link me to the page so i can help you solve this gap problem?
     
    crath, Jul 2, 2008 IP