0

I have this Array

array 
  0 => 
    array 
      'id' => 1
      'name' => Peter
      'desc' 
      'date'
  1 => 
    array 
      'id'
      'name'
      'desc' 
      'date'

And I want to echo for example Array[0]['name'] inside my input type text. This is my code up to now.

<select id="SelID">

                  <?php
                  foreach ($Array as $SubArray) {
                    echo '<option value ="'.$SubArray["id"].'">'.$SubArray["name"].'</option>';
                  }
                  ?>

                </select>

<input type="text" id="name" name="name"/>
                   <script type="text/javascript"> 

                  $('#SelID').change(function(){
                  $('#name').val(this.value);
                  });

This way, I'm changing my input's value to the index of the Select. What I need, is to replace it with $Array[Index(this.value I tried)]['name'] and get "Peter" inside my input. Thanks!

Gastón Vidal
  • 127
  • 2
  • 11
  • 1
    then don't use `val`. that returns the `value="..."` portion of the ` – Marc B May 19 '16 at 18:59
  • Possible duplicate of [Get selected text from a drop-down list (select box) using jQuery](http://stackoverflow.com/questions/1643227/get-selected-text-from-a-drop-down-list-select-box-using-jquery) – Cave Johnson May 19 '16 at 19:00

2 Answers2

1

$('#SelID').change(function(){
    $('#name').text($(this).find('option:selected').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="SelID">
    <option value="">Select...</option>
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>
<div id="name">

</div>
1

Using the option:selected, this problem might be solved. This is very general process.

$('#SelID').change(function(){
    val = $('#SelID option:selected').val();
    $('#name').val(val);
});

But you need to put the text of that option, so you need.

$('#SelID').change(function(){
    text = $('#SelID option:selected').text();
    $('#name').val(text);
});
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42