0

How to duplicate input value into dropdown option automatically?

I have text field where user will going to input some values (Let say an amount), Now, I want this values to be copied or duplicate in my dropdown option field automatically, particularly on my first "option", How do I do that?

<input id='appraisal_value' type='number'>

<select id='srp'>
    <option value='//This is where the users values from input `appraisal_value` goes '>Appraisal Value</option>
    <option value'<?php echo $BookValue; ?>'>Book Value</option>
</select>

2 Answers 2

2

$('#appraisal_value').change(function() {

  var value = $(this).val();
  $('#srp').prepend('<option>'+value +'</option>')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='appraisal_value' type='number'>

<select id='srp'>
  <option value='//This is where the users values from input `appraisal_value` goes '>Appraisal Value</option>

</select>

Use .prepend()

Description: Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.

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

1 Comment

Thank you. Worked like beast!
0
$("#appraisal_value").on("keyup", function(){
  $("#srp").children("option:first").val($(this).val());
});

This example shows when user keyup in the text field, value of first option in the select will replace with text field value.

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.