0

I'm using the following code to pass a value from a select option to a given input field.

$(".itemconditionmenu").change(function() {
  var conditionofitem = $(this).val();
  $("#meta_itemcondition").val(conditionofitem);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select id="itemcondition" class="itemconditionmenu">
  <option value="">Choose Condition</option>
  <option value="New">New</option>
  <option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New">

But how can I pass the value of the input field back to the select option value using jQuery?

11
  • Please include your html Commented Oct 17, 2019 at 8:41
  • You only need to change the selectors around. The same logic will work - assuming the select has an option matching the value entered. Commented Oct 17, 2019 at 8:41
  • you want to add a option in select field as per your input text? Commented Oct 17, 2019 at 8:47
  • @Amit i want the default value of the select box be the value of the input field. Commented Oct 17, 2019 at 8:51
  • 1
    but your code is working well, what do you want? I am still confused sorry. If you write 'abc' in your input field, it should be appear in select field? Commented Oct 17, 2019 at 8:52

4 Answers 4

1

I am not sure what you are exactly looking, however, as per my understanding I have modifed your code samples.. You can check below snippet.

$( document ).ready(function() {
    var conditionofitem = $("#meta_itemcondition").val();
    $(".itemconditionmenu").val(conditionofitem);
});

$(".itemconditionmenu").change(function() {
  var conditionofitem = $(this).val();
  if(conditionofitem == '') {
  	  $("#meta_itemcondition").val('Choose Condition');
   } else {
   	  $("#meta_itemcondition").val(conditionofitem);
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select id="itemcondition" class="itemconditionmenu">
  <option value="">Choose Condition</option>
  <option value="New">New</option>
  <option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New">

Please check and let me know if you have any further queries.

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

6 Comments

I want exactly what you did but vice versa. The select box must have the value of the input field not the opposite. Thank you
means if you type "New" then dropdown will be automatically selected "New" option, right?
No. When the page loads the input field has the value "new" as default. I want it so the select box has also the same value selected as default. So in summary there must be a way to tell the select box has the same value as the input field on page load. I dont touch at all the input field as in my form is hidden.
Please check now..is it okay?
Yeahhh! OK this is exactly what i wanted !!
|
1

Try this

$("#addOption").click(function(){
  $("#itemconditionmenu").append('<option value="'+$("#meta_itemcondition").val()+'" selected="selected">'+$("#meta_itemcondition").val()+'</option>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<select id="itemconditionmenu">
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<input id="meta_itemcondition" type="text" value=""></input>
<button id="addOption">ADD</button> 

Comments

1

Example for the same: Blur out your input and value option will be added to select dropdown

$(".itemconditionmenu").change(function() {
  var conditionofitem = $(this).val();
  $("#meta_itemcondition").val(conditionofitem);
});

$("#meta_itemcondition").blur(function() {

  var inputValue = $(this).val();
  var o = new Option(inputValue, inputValue);
  $(o).html(inputValue);
  $("#itemcondition").append(o);
  $("#itemcondition").val(inputValue);
});

$( document ).ready(function() {
  var value =  $("#meta_itemcondition").val();
  console.log(value)
  var alreadyOption = false;
  $("#itemcondition option").each(function()
    {
      alreadyOption = $(this).val() === value;
      if(alreadyOption) {
        $("#itemcondition").val(value);
      }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<select id="itemcondition" class="itemconditionmenu">
  <option value="">Choose Condition</option>
  <option value="New">New</option>
  <option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New"/>

6 Comments

It doesnt work. When you run the snippet the value of the select is "Choose Condition" instead of "New"
@Designer i thought you want it in option. I edited the code.
@Designer did you try now ??
Still doesnt work. When you run the script the value of the select box option is still "Choose condition" and not "new" as it should be.
Preloading itself you want it to be selected ?? can you edit input value At that time it takes new value
|
0

Here you can find inline functionality

  1. if you enter a value in textbox value append in drop-down
  2. if you select drop-down value auto set in textbox.

Note: add value in textbox change just input focus

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>



<select class="jsSelect" onchange="$('.jsTextBox').val($(this).val())"> 
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<input class="jsTextBox" onchange="if($(this).val()!=''){$('.jsSelect').append($('<option></option>').attr('value',$(this).val()).text($(this).val()));$('.jsSelect').val($(this).val());}" type="text" 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.