Mazz
Mazz

Reputation: 21

onchange form input grabs value and submits form

click to see the toggle input

I have a form input for item quantity, it's formatted to have toggles for increasing and decreasing the value

<input name="quantity" type="text" class="form-control text-center" id="quantity" value="<?php echo $crow[ 'quantity' ];?>" />

How could I use javascript to run a function on change or on click and grab it's value and submit a form?

Thanks for your help

I now have

function changeQuant(){
     var x = document.getElementById("quantity").value;
     document.getElementById('fquant').submit();
}

and the form submits on change

<form action="cartplusone.php" method="post" id="fquant" >                  
   <td class="input-qty"><input name="quantity" type="text" class="form-control text-center" id="quantity" onchange="changeQuant()" value="<?php echo $crow[ 'quantity' ];?>" /></td>
   <input name="newqval" type="hidden" id="newqval" value="">
</form>

How do I now best get the var x value into the hidden form field?

Upvotes: 0

Views: 829

Answers (2)

Nisal Edu
Nisal Edu

Reputation: 7591

You can use onchange

In your input add this onchange="myFunction()"

Following is an example with input type number

 function myFunction(){
     var x = document.getElementById("quantity").value;
     console.log(x);
}
<input type="number" id="quantity" onchange="myFunction()"/>

Upvotes: 1

Tobias Sch&#228;fer
Tobias Sch&#228;fer

Reputation: 1358

Maybe you're searching this:

<script>
$('#quantity').on('change', function() {
  alert($(this).val());
})
</script>

Note: This solution requires JQuery!

Upvotes: 1

Related Questions