Advertise with us

Values of variables to add in an array after I click the submit button?

Posted by  ben reitman
JQuery 
 1823  View(s)
Ratings:
Rate this:

Placed a value in an array variable after clicking submit button and the submit button will the one to trigger the arrayholder.push().

Variables that should be added to the array are those buttons that is clicked.

JAVASCRIPT


<script type="text/javascript">
val1=0;
val2=0;
val3=0;
val4=0;
arrayholder=new Array();
$("#id1").click(function(){
val1=val1+1;
$("#status").html(val1);
});
$("#id2").click(function(){
val2=val2+1;
$("#status").html(val2);
});
$("#id3").click(function(){
val3=val3+1;
$("#status").html(val3);
});
$("#id4").click(function(){
val4=val4+1;
$("#status").html(val4);
});
</script>


HTML CODE

    <button id="id1">ADD Val1 FROM id1</button>
    <button id="id2">ADD Val1 FROM id2</button>
    <button id="id3">ADD Val1 FROM id3</button>
    <button id="id4">ADD Val1 FROM id4</button>
<div id="status"></div>
<input type="submit" />

  1. Re: Values of variables to add in an array after I click the submit button?

    Instead of "submit" type, change into "button"  type and then use below line of code:

    <html>
    <head>
        <script type="text/javascript">
            function pushMe() {
                arrayholder.push(val1);
                document.getElementById("form1").submit();
            }
        </script>
    </head>
    <body>
        <form id="form1">
        <input id="btnPushMe" type="button" click="pushMe" />
        </form>
    </body>
    </html>

      Modified On Apr-03-2018 05:28:09 AM

Answer