0

i want copy text from select options into another automatically input , i have find this : http://jsfiddle.net/6khr8e2b/ but i cannot change it.

i want do this:

<select >
<option value="0">AAAA</option>
<option value="1">BBBB</option>
</select>

<select >
<option value="0">CCCC</option>
<option value="1">DDDD</option>
</select>

<input type="text" >

i will have two select and one input when i chose on selects AAAA and DDDD i want that in input value automatically type: AAAA DDDD

1

2 Answers 2

2

Javascript Solution

const selects = document.querySelectorAll('#select_1, #select_2');

const selectOne = selects[0];
const selectTwo = selects[1];

const input = document.getElementById('input_1');

Array.from(selects).forEach(select => {
  select.addEventListener('change', () => {
    input.value = `${selectOne.options[selectOne.selectedIndex].textContent} ${selectTwo.options[selectTwo.selectedIndex].textContent}`;
  });
});
<select id="select_1">
  <option value="0">AAAA</option>
  <option value="1">BBBB</option>
</select>

<select id="select_2">
  <option value="0">CCCC</option>
  <option value="1">DDDD</option>
</select>

<input id="input_1" type="text">

jQuery Solution

$(function() {
  const $selects = $('#select_1, #select_2');
  
  const $selectOne = $selects.eq(0);
  const $selectTwo = $selects.eq(1);

  const $input = $('#input_1');

  $selects.on('change', () => {
    $input.val(`${$selectOne.find(':selected').text()} ${$selectTwo.find(':selected').text()}`);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="select_1">
  <option value="0">AAAA</option>
  <option value="1">BBBB</option>
</select>

<select id="select_2">
  <option value="0">CCCC</option>
  <option value="1">DDDD</option>
</select>

<input id="input_1" type="text">

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

5 Comments

Can you try again. I just made an adjustment.
now working, but how do that it work only for id="select_1" id="select_2" id="input_1"
I don't quite understand your question. Can you rephrase that?
@Geo1313 I think I understand now. Can you check my update and see if that addressed your question?
Check my latest update. I've provided a jQuery solution as well.
0

it will helpful to you

function getinput(){

var val="";
val=$("#select_1 option:selected").text()+ " "+$("#select_2 option:selected").text()
$("#input_1").val(val);
}
$(function(){
   $(".select").change(function(){
      var val="";
      val=$("#select_1 option:selected").text()+ " "+$("#select_2 option:selected").text()
      $("#input_1").val(val);

   });
    $(".select").trigger("change");
})
select{
width:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select  class="select"  id="select_1">
<option value="0">AAAA</option>
<option value="1">BBBB</option>
</select>

<select class="select" id="select_2">
<option value="0">CCCC</option>
<option value="1">DDDD</option>
</select>

<input type="text" id="input_1" >

3 Comments

can do same without onchange="getinput()"
how can i add Third <select class="select" id="select_3">
@Geo1313 ` val=$("#select_1 option:selected").text()+ " "+$("#select_2 option:selected").text()+" "+$("#select_3 option:selected").text()`

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.