-2

I want to copy value of input fields to my drop down using jQuery. I have four option fields here with respective ids, when user enters any text on following field my drop down get value from it in real time .

My code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<label>option a</label>
<input type="text" id="opa" size="30" class="text-input" />
<br>
<label>option b</label>
<input type="text" id="opb" size="30" class="text-input" />
<br>
<label>option c</label>
<input type="text" id="opc" size="30" class="text-input" />
<br>
<label>option d</label>
<input type="text" id="opd" size="30" class="text-input" />
<br>


<input type="text" id="a1" name="">

<select name="ra" class="form-control" id="ra">
  <option value="" >---Select right option ---</option>
  <option id="a" ></option>
  <option id="b" ></option>
  <option id="c" ></option>
  <option id="d"  ></option>
</select>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Hamza Mustafa
  • 101
  • 1
  • 5
  • What have you tried so far using jQuery? Please post it's code as well. @HamzaMustafa – SE_User Aug 07 '17 at 07:56
  • i dont have so many idea about jquerey , $("#ra").change(function(){ $("#opa").val($(this).val("#a1")); }); – Hamza Mustafa Aug 07 '17 at 07:57
  • I would strongly suggest you to learn the basics of jQuery. Here are few resources for the same: [jQuery Tutorial](https://www.tutorialspoint.com/jquery/index.htm) – SE_User Aug 07 '17 at 08:00

2 Answers2

1

You can use this code below to achive what you want.

$("input[id^=op]").keyup(function() {
  var id = $(this).attr("id").replace("op","");
  $("#ra #" + id).val($(this).val()).text($(this).val());
})

Note I've didn't noticed that it was marked as duplicate before I pressed submit to the answer, but I'll leave it anyway

$("input[id^=op]").keyup(function() {
  var id = $(this).attr("id").replace("op","");
  $("#ra #" + id).val($(this).val()).text($(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>option a</label>
<input type="text" id="opa" size="30"  class="text-input" />
<br>
<label>option b</label>
<input type="text" id="opb" size="30"  class="text-input" />
<br>
<label>option c</label>
<input type="text" id="opc" size="30"  class="text-input" />
<br>
<label>option d</label>
<input type="text" id="opd" size="30"  class="text-input" />
<br>


<select name="ra" class="form-control" id="ra">
              <option value="" >---Select right option ---</option>
              <option id="a" ></option>
              <option id="b" ></option>
              <option id="c" ></option>
              <option id="d"  ></option>
             </select>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
1

I create the following fiddle to update option text and value on keyup event:

$('input').on('keyup', function() {
  var id = $(this).attr('id');
  $('#' + id[id.length - 1]).text(this.value);
  $('#' + id[id.length - 1]).val(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<label>option a</label>
<input type="text" id="opa" size="30" class="text-input" />
<br>
<label>option b</label>
<input type="text" id="opb" size="30" class="text-input" />
<br>
<label>option c</label>
<input type="text" id="opc" size="30" class="text-input" />
<br>
<label>option d</label>
<input type="text" id="opd" size="30" class="text-input" />
<br>


<input type="text" id="a1" name="">

<select name="ra" class="form-control" id="ra">
  <option value="" >---Select right option ---</option>
  <option id="a" ></option>
  <option id="b" ></option>
  <option id="c" ></option>
  <option id="d"  ></option>
</select>
Alex Char
  • 32,879
  • 9
  • 49
  • 70