Changing a number with onClick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bob parker
    New Member
    • Nov 2011
    • 6

    #1

    Changing a number with onClick

    ok so i have the code listed below and i want it so that when i click the button x goes up by 60 for every click. then, this part i need but not as much, when it reaches y i would like it to execute a command to change a pic.
    Code:
    <input name="submit" type="button" value="button" onclick="button" />
    <script language="JavaScript" type="text/javascript">
    
    var x;
    var y;
                                                                x = 0;
    y = 600;
    
    document.write(x);
    document.write("/");
    document.write(y);
    
    </script>
    Last edited by acoder; Nov 17 '11, 09:07 AM. Reason: Added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    first of all, document.write( ) doesn’t work with(in) events.
    Code:
    <input id="submit" type="button" value="button">
    <input id="display" type="text" readonly>
    Code:
    function changePicOrNot()
    {
        if (60 == this.clicked++) {
            changeSRC();
        }
        display.value = this.clicked + "/60";
    }
    var display = document.getElementById("display");
    var submit  = document.getElementById("submit");
    submit.clicked = 0;
    // all browsers except IE < 9
    submit.addEventListener("click", changePicOrNot, true);

    Comment

    • bob parker
      New Member
      • Nov 2011
      • 6

      #3
      thanks but the pic isnt changing and i want the number to stay there even if i refresh the page.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You have to write a function to change the source of the pic. If you refresh the page, you can't keep the value unless you save it in a cookie or on a server.

        Comment

        • bob parker
          New Member
          • Nov 2011
          • 6

          #5
          well can you tell me that function is then?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I don't give out code unless the poster shows an attempt.

            Comment

            • bob parker
              New Member
              • Nov 2011
              • 6

              #7
              this is what i have.
              Code:
              <script language="JavaScript">
                  function changeSRC() {
                      document.getElementById("x").src = "pic2.png"
                  }
              
              </script>
              <img id="x" src="pic1.jpg" alt="x" onchange="changeSRC()" />
              Last edited by Dormilich; Nov 17 '11, 06:10 AM. Reason: please use [CODE] [/CODE] tags when posting code

              Comment

              • bob parker
                New Member
                • Nov 2011
                • 6

                #8
                thanks but my code doesnt seem to work.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  I don’t think an image having a change event would make sense …

                  onchange
                  User committed a change to the value of element (form control).

                  Comment

                  • bob parker
                    New Member
                    • Nov 2011
                    • 6

                    #10
                    it did work i just forget to clear my cache. and your right i dont need the on change as it doesnt do anything for this.

                    Comment

                    Working...