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

3

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0
$('select').on('change', function (e) {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    $('input[name="signoff_date"]').val(valueSelected);
});

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.