adrian pascua adrian pascua - 26 days ago 7
PHP Question

How to add onClick in html option?

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

Answer

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">

Comments