[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript visiblity

Posted on 2008-06-26
3
Medium Priority
?
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 21874815
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
ID: 21874830
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 500 total points
ID: 21904834
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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