function call executing only one line onclick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarantulus
    New Member
    • May 2007
    • 114

    function call executing only one line onclick

    Hi guys,

    My JS is terrible so forgive me, but I'm having trouble with a function to hide/show divs, the code is as follows:

    Code:
    var state
    state=0;
    function edit(){
    if(state==0){
    state=1;
    document.getElementById('editor_area').style.display='inline';
    document.getElementById('display_area').style.display='none';
    			
    
    		document.getElementById('jump').focus()
    };
    else{
    state=0;
    document.getElementById('editor_area').style.display='none';
    document.getElementById('display_area').style.display='inline';
    
    };
    };
    however when I call edit() (button onclick) it only performs the first show/hide action after resolving the if (for example state==1, would result in "state=0;
    document.getEle mentById('edito r_area').style. display='none'; " being actioned);

    how can I fix this?
    I hope this makes sense.

    thanks in advance
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Try this and tell me if there's any success..[CODE=javascript]var state = false;
    function edit() {
    document.getEle mentById('edito r_area').style. display = state ? 'none' : '';
    document.getEle mentById('displ ay_area').style .display = state ? '' : 'none';
    state = !state;
    if (!state) document.getEle mentById('jump' ).focus();
    }[/CODE]

    Comment

    • Tarantulus
      New Member
      • May 2007
      • 114

      #3
      Originally posted by hsriat
      Try this and tell me if there's any success..[CODE=javascript]var state = false;
      function edit() {
      document.getEle mentById('edito r_area').style. display = state ? 'none' : '';
      document.getEle mentById('displ ay_area').style .display = state ? '' : 'none';
      state = !state;
      if (!state) document.getEle mentById('jump' ).focus();
      }[/CODE]
      No dice, the onclick doesn't activate anything now.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by Tarantulus
        No dice, the onclick doesn't activate anything now.
        ow.. it's hard to believe that it got even worse..

        There's no space in disp lay, make sure it's not causing the problem.

        And replace the empty quotes with inlay.. just in case you have provided display:none as default in css.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You're using a single equal, instead of double equals:[CODE=javascript]var state = false;
          function edit() {
          document.getEle mentById('edito r_area').style. display = state ? 'none' : '';[/CODE]

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by acoder
            You're using a single equal, instead of double equals:[CODE=javascript]var state = false;
            function edit() {
            document.getEle mentById('edito r_area').style. display = state ? 'none' : '';[/CODE]
            ...umm.... state is itself a boolean. I'm not comparing it.

            Comment

            • Tarantulus
              New Member
              • May 2007
              • 114

              #7
              Thanks for this, but it's still misbehaving...

              now the "display_ar ea" div is disappearing, but the "editor_are a" div wont appear :S.

              this is really frustratiing, it's not even throwing any errors that I could investigate!

              Thanks again

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by Tarantulus
                Thanks for this, but it's still misbehaving...

                now the "display_ar ea" div is disappearing, but the "editor_are a" div wont appear :S.

                this is really frustratiing, it's not even throwing any errors that I could investigate!

                Thanks again
                There was a space in display of editor area code line too.. Did you delete that?

                Comment

                • Tarantulus
                  New Member
                  • May 2007
                  • 114

                  #9
                  Originally posted by hsriat
                  There was a space in display of editor area code line too.. Did you delete that?
                  yep, all cleaned up, the spaces have been removed and I've put 'inline' instead of ' '

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by Tarantulus
                    yep, it now reads as
                    Code:
                     var state = false;
                    function edit() {
                        document.getElementById('editor_area').style.display = state ? 'none' : 'inline';
                        document.getElementById('display_area').style.display = state ? 'inline' : 'none';
                        state == !state;
                        if (!state) document.getElementById('jump').focus();
                    }
                    lol... that is funny... do u think I gave those spaces intentionally?


                    But anyhow, is this working now or not?...
                    This should definitely work. If it doesn't, then there's some other problem with the code.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by hsriat
                      ...umm.... state is itself a boolean. I'm not comparing it.
                      Ah, I see, sorry about that.

                      @Tarantulus, post your HTML code, or a link.

                      Comment

                      • Tarantulus
                        New Member
                        • May 2007
                        • 114

                        #12
                        Originally posted by hsriat
                        lol... that is funny... do u think I gave those spaces intentionally?
                        Of course not, I'm dumb, but not that dumb!
                        Originally posted by hsriat
                        But anyhow, is this working now or not?...
                        This should definitely work. If it doesn't, then there's some other problem with the code.
                        you're right, it was a problem with my DIV positioning :)

                        Thanks for your help, really appreciate it.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by hsriat
                          lol... that is funny... do u think I gave those spaces intentionally?
                          There's a bug with the code tags. The fix has been in the pipeline for some time. Hopefully it'll come out of the tunnel soon...

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by Tarantulus
                            you're right, it was a problem with my DIV positioning :)
                            Can you post the two versions, i.e. what exactly was wrong with the positioning?

                            Comment

                            • hsriat
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1653

                              #15
                              Originally posted by acoder
                              There's a bug with the code tags. The fix has been in the pipeline for some time. Hopefully it'll come out of the tunnel soon...
                              I know that... and that doesn't upset me.

                              But it sometimes causes confusions for the new users.

                              regards

                              Comment

                              Working...