'Update input field when the value of a select changes?

Update input field event onchange?

$A = array(A,B,C,D);

<select name="A" id="A" onchange="change_val()">
<?php 
foreach($A as $key=>$val){?>
    <option value="<?php echo $key?><?php echo $val?"></option>
<?php }?>
 </select>
<input name="seleted_value" id="seleted_value" value="Hello first time" >

I want to write a onchange method change_val() that do this task:

if select A ( by default ) show value 'Hello first time' into input field seleted_value

else show value have selected into input field seleted_value AND Disable this input field for read only.

Anybody could guide me how to do this?

  if selected= A =>show 'Hello first time'
  if selected= B =>show 'B'
  if selected= C =>show 'C'
  if selected= D =>show 'D'


Solution 1:[1]

jQuery

I suggest using jQuery for this. Then your code would look something like this:

$(function()
{
    $('select#A').change(function()
    {
        var this_value = $(this).val();

        if(this_value == 0)
        {
            //you made a typo (seleted), so i did this too on purpose
            $('input#seleted_value').val('Hello first time');
        }
        else
        {
            $('input#seleted_value').val(this_value);
            $('input#seleted_value').attr('disabled', 'disabled');
        }
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1