Solved

Javascript visiblity

Posted on 2008-06-26
3
455 Views
Last Modified: 2008-07-16
I'm trying to write a simiple function that makes a div visible or invisible in Javascript,
Please show me whats wrong with this code
<head>
    <script>
        function showHide(elemShow)
        {
            
             if (document.getElementById(elemShow).style.visibility = 'visible'){
                        alert('hello again');
                        document.getElementById(elemShow).style.display = 'none';                   
                    }
             else if (document.getElementById(elemShow).style.display = 'none') {  
                        document.getElementById(elemShow).style.display = 'block';
                        document.getElementById(elemShow).style.visibility = 'visible';
                         alert('hello');
                              }
             return true;
             }
    </script>
</head>
<body>
    <div style="float:left;"><input type=radio name="address_type" value="1" checked onClick="showHide('box');"></div>
    <div style="float:left;">Postal Box</div>
      <div style="clear:both;"></div>

    <div id="box" style="display: none;">
        <table>
            <tr>
                <td>The Postal Box fields here</td>
            </tr>
        </table>
    </div>
</body>
0
Comment
Question by:healthmanagement
  • 2
3 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
It helps if you simplify things by removing duplication.  For example:

A big problem is that in order to test for two string values being equal, you need to use ==, not the single = used for assignment
<script type='text/javascript'>

  function showHide( id ) {

    var ele = document.getElementById( id );

    var result = false;

    if ( ele ) {

      if ( ele.style.visibility == 'visible' ) {

        alert( 'hello again' );

        ele.style.visibility = 'hidden';

        result = true;

      } else if ( ele.style.display == 'none' ) {

        ele.style.display = 'block';

        ele.style.visibility = 'visible';

        alert( 'hello' );

        result = true;

      }

    }

    return result;

</script>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
oops, I forgot the closing '}' ... sorry
<script type='text/javascript'>

  function showHide( id ) {

    var ele = document.getElementById( id );

    var result = false;

    if ( ele ) {

      if ( ele.style.visibility == 'visible' ) {

        alert( 'hello again' );

        ele.style.visibility = 'hidden';

        result = true;

      } else if ( ele.style.display == 'none' ) {

        ele.style.display = 'block';

        ele.style.visibility = 'visible';

        alert( 'hello' );

        result = true;

      }

    }

    return result;

  }

</script>

Open in new window

0
 
LVL 3

Accepted Solution

by:
itbeme earned 125 total points
Comment Utility
You got to much code there for that.

Here this guy will tell you 7 ways
http://www.dustindiaz.com/seven-togglers/

something like this is all you need
function toggle(obj) {
      var el = document.getElementById(obj);
      if ( el.style.display != 'none' ) {
            el.style.display = 'none';
      }
      else {
            el.style.display = '';
      }
}

also make sure you're using a javascript bugger.  You're going to have a lot of headaches if you're debugging javascript by watching for the little script error icon.  At least use FF's script debugger.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now