adding a value "1" to all fields with same name

Discussion in 'JavaScript' started by worldofrugs, Nov 1, 2010.

  1. #1
    From a database, I have a form that shows a list of items.
    These items have an order number that I can change on this form
    .
    I like to make a very simple button, that just adds +1 to the current values in the fields.
    The form fields are all named the same, "OrderNo" and let's call the form "MyForm", and the fields contain a number.
    In total there's about 20 input fields (all "OrderNo"), but it can sometimes be more or less.

    So in simple:
    A 1-click button, that adds "1" to ALL the field values.

    It sounds simple (and I think it has to be??), but cannot seem to figure it out.
    Anyone a suggestion on this?
    Thanks in advance!
     
    worldofrugs, Nov 1, 2010 IP
  2. S1M

    S1M Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use the dom to loop through the form fields and check the field name for the text you want. If it's there, increment the value for that field by one.
     
    S1M, Nov 2, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    
        <form name="myform">
            <input type="text" name="OrderNo" value="1" />
            <input type="text" name="OrderNo" value="12" />
            <input type="text" name="OrderNo" value="3" />
            <input type="button" id="BtnAddOne" value="+1" />
        </form>
        <script type="text/javascript">
            var myTxts = document.getElementsByName("OrderNo");
            document.getElementById("BtnAddOne").onclick = function() {
                for(var i=0;i<myTxts.length;i++) myTxts[i].value = parseInt(myTxts[i].value) + 1;
            }
        </script>
    
    Code (markup):
     
    camjohnson95, Nov 7, 2010 IP