www.webdeveloper.com
Results 1 to 4 of 4

Thread: simple hide function is not working as intended

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Location
    antartica
    Posts
    3

    Red face simple hide function is not working as intended

    Hi all,

    First of aal sorry for my ignorance, but i just started with javascript.
    I made small function with that hides or shos a div. In addition to that i made a small button that class that function.

    Maybe someone could take a look at it and point me in the right direction:
    this i put in my <head> </head>
    Code:
    <script type="text/javascript">
                function hide_me(haha){
                    var el = document.getElementById(haha)
                    if (el.style.display != 'none'){
                        el.style.display = 'none';
                    }else{
                        el.style.display = '';
                    }
                }
    </script>
    and in the body i have this
    Code:
    <div id="haha">
          <p>moehaha i am invisible</p>
    </div>
        <input type="submit" onclick="hide_me(haha)" value="press me" />

  2. #2
    Join Date
    Oct 2010
    Location
    antartica
    Posts
    3
    I solved it myself by adding a single quotes around the value in the button.
    Like:
    Code:
    <input type="button" onmouseover="hideDiv('mydiv')" value="HIDE!!!"/>
    But there is one thing that is really confusing me. Why do i have to use single quotes here and why a function like below doesn't use them? This maybe a noobie question, but if someone could explain me why it would help me alot, I searched for parameters javascript functions, but I didn't found a direct answer to this question.

    Code:
    function toggle(obj){
    // etc
    }

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    In the onmouseover function you are passing a string, hence the need for quotes.
    The function argument is a reference to the string.
    It's a bit like defining a variable
    Code:
    var foo = 'bar';
    The name of the variable isn't quoted, but it's string value is.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Oct 2010
    Location
    antartica
    Posts
    3
    Ah, that makes sense. Thanks alot for the explanation.
    Cheers!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles