0

I have this HTML code

<form method="post" action="cashier.php" enctype="form-data/multipart">    
    <input required id="amount" type="number" min="0" class="form-control" name="txtamount" placeholder="Enter Payment Here..." />
    <input required id="cash" type="number" min="document.getElementById('amount').value" class="form-control" name="txtcash" placeholder="Enter Cash Here..." />
    <input type="submit" class="btn btn-primary pull-right" name="btnenroll" value="Done" />
</form>

If I type 1000 on id="amount", the id="cash" min will be 1000.

But it doesn't work.

2
  • You can't have dynamic value in min attribute in pure HTML. You'll need a JS (and possibly a library) for this. Commented Sep 21, 2015 at 12:10
  • 1
    min and max attributes can fill only with a number or a date depending the type. Maybe you are interesting in HTML5 constraint validation w3.org/TR/2011/WD-html5-20110525/… Commented Sep 21, 2015 at 12:13

4 Answers 4

4

The most straightforward way is to add an onchange handler to the payment input, such as:

<form method="post" action="cashier.php" enctype="form-data/multipart">    
    <input required id="amount" type="number" min="0" class="form-control" name="txtamount" placeholder="Enter Payment Here..." 
        onchange="document.getElementById('cash').min=this.value;"/>
    <input required id="cash" type="number" min="document.getElementById('amount').value" class="form-control" name="txtcash" placeholder="Enter Cash Here..." />
    <input type="submit" class="btn btn-primary pull-right" name="btnenroll" value="Done" />
</form>

This gets you the functionality you are looking for without needing to include a third party library like jQuery.

Sign up to request clarification or add additional context in comments.

Comments

1

In this case you should use Jquery onchange or keypress

 $("#cash").attr({
       "min" : $("#amount").val()
 });

Comments

0

If you are using jQuery, use the below,

$("#amount").change(function() {
  $("#cash").attr('min',$("#amount").val());
});

Refer - https://api.jquery.com/change/

2 Comments

keypress? What about copy-pasting?
@Pavlo - changed to change
0

I don't think you can put javascript in min, and also you have a pair of double quote in another pair of double quote, which will break the the tag (You can inspect element and you will figure out.)

So the idea is when the value of amount element change, we change cash's min value. So we can add a event listener to amount element. Whenever it is changed, set the min for cash element. Here is a working sample code:

<form method="post" action="cashier.php" enctype="form-data/multipart">    
    <input required id="amount" type="number" class="form-control" name="txtamount" placeholder="Enter Payment Here..." />
    <input required id="cash" type="number" class="form-control" name="txtcash" placeholder="Enter Cash Here..." />
</form>

<script>
    function setMin() {
        var amount = document.getElementById("amount");
        var cash = document.getElementById("cash");
        cash.min = amount.value;
    }

    var trigger = document.getElementById("amount");
    trigger.addEventListener("change", setMin, false);
</script>

As you can see, I removed your js in html and add a event listener. Hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.