Reputation: 123
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!
Upvotes: 0
Views: 576
Reputation:
$('#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>
Upvotes: 1
Reputation: 9583
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);
});
Upvotes: 1