Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2006
    Posts
    247
    Thanks
    13
    Thanked 26 Times in 24 Posts

    Show/Hide element?

    Alright, I got the basic idea (which is relatively easy):

    Code:
    function show_el(id)
    {
    	var obj = document.getElementById(id);
    	obj.style.display = "block";
    }
    function hide_el(id)
    {
    	var obj = document.getElementById(id);
    	obj.style.display = "none";
    }
    The problem is, you need 2 different links, one to show the element, one to hide it. What I want is one link that changes depending on whether or not the element is showing: If it's showing, the link will be "Hide Element", if it's not, it'll be "Show Element."

    I've seen this done a lot, mainly in expanding/collapsing, which is basically what I'm doing. I imagine it's not all that hard, but I just can't seem to figure it out.

    Thanks.
    Last edited by Majoracle; 02-20-2007 at 08:53 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    css +
    Code:
    .hide {
    	display: none;
    }
    js +
    Code:
    function toggle_el(id) {
    	var obj = document.getElementById(id);
    	obj.className = (obj.className == 'hide') ? '' : 'hide';
    }
    It's recommended that you try to avoid making direct presentational adjustments in the js. Add rules to the CSS and use js to adjust class attribute values, as above.

    Ideally, you'd use js functions which can account for the possible presence of existing class attributes. I can show you how, if you're interested.

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    247
    Thanks
    13
    Thanked 26 Times in 24 Posts
    Quote Originally Posted by Bill Posters View Post
    Ideally, you'd use js functions which can account for the possible presence of existing class attributes. I can show you how, if you're interested.
    Thank you, and yes, please do.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •