Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Js counter wht no work

    THIS IS MY CODE


    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">
    var 
    count=0;
    function 
    plus(f) {
    count += 1;
    f.myText.value count;
    }

    function 
    minus(f) {
    if(
    f.myText.value>0)
    {
    count -= 1;
    f.myText.value count; }
    }  
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="myText" value=""><br>
    <input type="button" name="myBut" value="+1" onClick="plus(form)">
    <input type="button" name="myBut" value="-1" onClick="minus(form)">
    </form>
    </body>
    </html> 
    input +2 buttons that make + and -
    that work
    what it want to do it 20 input like this and i dont want to copy the function
    so... i tried to send the name of the text input in the function like this

    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">
    var 
    count=0;
    function 
    plus(f,k) {
    count += 1;
    f.k.value count;
    }

    function 
    minus(f,k) {
    if(
    f.k.value>0)
    {
    count -= 1;
    f.k.value count; }
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="myText" value=""><br>
    <input type="button" name="myBut" value="+1" onClick="plus(form,myText)">
    <input type="button" name="myBut" value="-1" onClick="minus(form,myText)">



    </form>
    </body>
    </html> 
    and this is not work
    why please ?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You must pass the value of the textbox and convert it form a string to a number.

    Code:
    <script type = "text/javascript">
    
    function plus(f,val) { 
    var count = Number(val);
    count += 1; 
    f.myText.value = count; 
    } 
    function minus(f,val) { 
    var count = Number(val);
    count -= 1; 
    f.myText.value = count; 
    } 
    
    </script> 
    </head>
     
    <body> 
    <form> 
    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
    <input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)"> 
    </form>
    Quizmaster: In seafood on a restaurant menu, the French word "poisson" translates into English as what?
    Contestant: Chicken
    Last edited by Philip M; 07-19-2012 at 01:13 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You must pass the value of the textbox and convert it to a number.


    Code:
    <html> 
    <head> 
    
    <script type = "text/javascript">
    
    function plus(f) { 
    var count = Number(document.forms[0].myText.value);
    count += 1; 
    document.forms[0].myText.value = count; 
    } 
    function minus(f) { 
    var count = Number(document.forms[0].myText.value);
    count -= 1; 
    document.forms[0].myText.value = count; 
    } 
    
    </script> 
    </head>
     
    <body> 
    <form> 
    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(form)"> 
    <input type="button" name="myBut" value="-1" onclick="minus(form)"> 
    </form>

    Quizmaster: In seafood on a restaurant menu, the French word "poisson" translates into English as what?
    Contestant: Chicken
    i or you not understand
    i want to do 20 like this input text with +-
    if i will send to function like you do i will need 2 funcrion for every input box

    i want 20 like this
    <input type="text" name="myText" value="1"><br>
    <input type="button" name="myBut" value="+1" onClick="plus(form,myText)">
    <input type="button" name="myBut" value="-1" onClick="minus(form,myText)">

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I am not sure that I do understand you. Is this what you want?

    Code:
    <script type = "text/javascript">
    
    function plus(fld) {
    var count = Number(fld.value);
    count += 1; 
    fld.value = count; 
    } 
    function minus(fld) { 
    var count = Number(fld.value);
    count -= 1; 
    fld.value = count; 
    } 
    
    </script> 
    </head>
     
    <body> 
    <form> 
    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(myText)">
    <input type="button" name="myBut" value="-1" onclick="minus(myText)"> 
    <br><br>
    <input type="text" name="myText1" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(myText1)">
    <input type="button" name="myBut" value="-1" onclick="minus(myText1)"> 
    
    
    </form>
    Last edited by Philip M; 07-19-2012 at 01:26 PM. Reason: Revised

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I am not sure that I do understand you. Is this what you want?

    Code:
    <script type = "text/javascript">
    
    function plus(f,val) { 
    var count = Number(val);
    count += 1; 
    f.myText.value = count; 
    } 
    function minus(f,val) { 
    var count = Number(val);
    count -= 1; 
    f.myText.value = count; 
    } 
    
    </script> 
    </head>
     
    <body> 
    <form> 
    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
    <input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)"> 
    </form>
    i want that work


    PHP Code:
    <script type "text/javascript">

    function 
    plus(f,val) { 
    var 
    count Number(val);
    count += 1
    f.myText.value count

    function 
    minus(f,val) { 
    var 
    count Number(val);
    count -= 1
    f.myText.value count


    </script> 
    </head>
     
    <body> 
    <form> 
    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
    <input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)"> 

    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
    <input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)"> 

    <input type="text" name="myText" value="0"><br> 
    <input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
    <input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)"> 
    </form> 
    and not copy the function 3 times

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    What is wrong with the code I gave you in Post#4 (as revised)?

    You cannot have three form fields with the same name myText. You must give them unique names.
    Last edited by Philip M; 07-19-2012 at 02:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Jan 2011
    Posts
    52
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by yossidd View Post
    input +2 buttons that make + and -
    that work
    what it want to do it 20 input like this and i dont want to copy the function
    so... i tried to send the name of the text input in the function like this


    i want that work


    and not copy the function 3 times
    Code:
    <doctype html>
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <script type = "text/javascript">
    
    function plusOrMinus( evt ) 
    { 
      var e = evt || window.event,
          elem = evt.target || evt.srcElement,
          val = Number( elem.value ),
          field;
        
      while( ( field = elem.previousSibling ) && field.type !== 'text' )
        elem = field;
      
      if( field )
        field.value = Number( field.value ) + val;      
    } 
    
    </script> 
    
    </head>
     
    <body> 
    <form onclick = "plusOrMinus( event )"> 
      <input type="text" name="myText" value="0">
      <input type="button" value="+1" >
      <input type="button" value="-1" ><br>  
    
      <input type="text" name="myText" value="0">
      <input type="button" value="+1" >
      <input type="button" value="-1" ><br>  
    
      <input type="text" name="myText" value="0">
      <input type="button" value="+1" >
      <input type="button" value="-1" ><br>  
    </form>
    </body>  
    </html>


  •  

    Posting Permissions

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