skip to Main Content

I want to display the amount in the textbox based on the date selected.For weekdays amount is 200 on sundays amount is 500. How can I do it in jquery? Whether it is possible to do?
How can I do it in jquery?

<input type="date" name="mass_date" id="txtDate" required="required" class="col-md-12" />

<input type="text" class="form-control" id="slDay" name="amount"  />    

2

Answers


  1. Use getDay. Add a event listener to to input change then each time the input changes create a new Date and then use the getDay function that returns the day number (0 for Sunday).

    Then you can put a conditional statement to change the form-control selector.

    $(function() {
      $("#txtDate").change(function() {
        var selDate = new Date(this.value);
        if (selDate.getDay() == 0) { //If sunday, can change your logic here
          $(".form-control").val(5000);
        } else {
          $(".form-control").val(2000);
        }
      })
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="date" name="mass_date" id="txtDate" required="required" class="col-md-12" />
    
    <input type="text" class="form-control" id="slDay" name="amount" />
    Login or Signup to reply.
  2. A simple version

    $(function() {
      $("#txtDate").change(function() {
        var dow = new Date(this.value).getDay();
        $(".form-control").val(dow === 1 || dow ===6 ? 2000 : 5000);
      })
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="date" name="mass_date" id="txtDate" required="required" class="col-md-12" />
    
    <input type="text" class="form-control" id="slDay" name="amount"  />
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search