skip to Main Content

I want to autofill the textbox with the amount when a dropdown is selected.. Drop down consists of first installment, second installment. When first installment is selected 1000rs has to be displayed in the textbox.. Whether it is possible to do… I want to insert option value and amount in db too

<select name="installment">
<option value="First installment">First Installment</option>
<option value="Second Installment">Second Installment</option>
</select>
<input type="text" name="amount">

2

Answers


  1. I just added an event listener to the select, so on change the input will get the value you want depend on the value of the select.

    document.getElementById('selectName').addEventListener("change", function(){
      if(document.getElementById('selectName').value == "First installment"){
      document.getElementById('amount').value = '1000rs'
      }
      if(document.getElementById('selectName').value == "Second Installment"){
      document.getElementById('amount').value = '5000rs'
      }
    });
    

    code pen example:
    https://codepen.io/Elnatan/pen/RwabKwG

    to upload it to the DB i need to know what backend are you using.
    but general you need to create a form (method post) or send it as a post request to the backend. then in the back end to take this value and store it to DB.

    Login or Signup to reply.
  2. For the auto filling of amount you can try this;

    Modify the HTML as follows

    <select name="installment" onchange="autofill(this.value);">
    <option value="1000rs">First Installment</option>
    <option value="2000rs">Second Installment</option>
    <option value="3000rs">Third Installment</option>
    </select>
    <input type="text" value="0" id="amount"/>
    

    While Javascript is

    <script>
    function autofill (amt){
    var txt = document.getElementById("amount");
    txt.value=amt;
    }
    </script>
    

    As for the saving to Database, what language are you using?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search