0

What im trying to do is when the <option> is clicked the value will be displayed in the <input type='text'>

here is my code right now:

<select id='months'>
   <option value='9' id='months9'>9 Months</option>
   <option value='10' id='months10'>10 Months</option>
   <option value='11' id='months11'>11 Months</option>
   <option value='12'id='months'>12 Months</option>
</select>

if($months == '9'){
   echo 'today +9 months';
}
if($months == '10'){
   echo 'today + months';
}
if($months == '11'){
   echo 'today +11 months';
}
if($months == '12'){
   echo 'today +12 months';
}

<input type='text' name='signoff_date'>

NOTE: I know this code will not work i'm just giving you idea how my program works.

I hope you understand it. Thanks

adrian pascua
  • 31
  • 1
  • 3
  • 9

3 Answers3

1

To get the current value of the select, use the onchange event:

function updateValue(value)
{
    document.getElementById('signoff_date').value = value
}

// set a defalt value
updateValue(document.getElementById('months').value)
<select id='months' onchange="updateValue(this.value)">
   <option value='9' id='months9'>9 Months</option>
   <option value='10' id='months10'>10 Months</option>
   <option value='11' id='months11'>11 Months</option>
   <option value='12'id='months'>12 Months</option>
</select>

  
<input type='text' name='signoff_date' id="signoff_date">
fuzzyCap
  • 391
  • 4
  • 14
0
$('select').on('change', function (e) {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    $('input[name="signoff_date"]').val(valueSelected);
});
Rafał Cz.
  • 735
  • 9
  • 19
0

Untested but something like this perhaps - assign an onchange event handler rather than a click handler.

<script type='text/javascript'>
    function evtoption(e){
        var el=e.target ? e.target : e.srcElement;
        var selected=el.options[ el.options.selectedIndex ];
        var value=selected.value;
        var txt=selected.text;
        var id=selected.id;

        document.querySelectorAll('input[name="signoff_date"]')[0].value=id+value+txt;
    }

    document.getElementById('months').addEventListener( 'change', evtoption, false );
</script>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46