Populate textarea on <select> onchange in PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Populate textarea on <select> onchange in PHP

Post by simonmlewis »

I want to have a dropdown <select> menu, in the menu the <option value=''> needs to have the full context of a response someone is giving, and the display of it <option value='test'>this display</option> in the dropdown itself.

Above/below it will be a <textarea></textarea>

When the dropdown option is selected, I want the "value" of the selected option to suddenly appear in the <textarea>.
It would be good if they changed their selection, that what is in textarea clears, and is replaced by the selection.

The person who posted this is on the similar track to me:
http://stackoverflow.com/questions/1164 ... e-textarea
But I want it for a <select> dropdown.

Easy or not??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Populate textarea on <select> onchange in PHP

Post by simonmlewis »

I often do this - post it here and find the perfect answer - still, at least I have a record of it.

Code: Select all

    <script type="text/JavaScript">

    function showText(){
    var F, opt;
    F = document.oForm;
    opt = F.oSel.options;

    F.oTxt.value = opt[opt.selectedIndex].value;

    }
    </script>

    </head>
    <body>

    <form name="oForm">

    <select name="oSel" 
    onchange="showText()">

    <option value='This is the content'>lorem</option>
    <option value='Actually this is the right stuff'>ipsum</option>
    <option value='No this is the right wording'>dolar</option>

    </select>

    <textarea name="oTxt" cols="10" rows="5">
    </textarea>

    </form>

   
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Populate textarea on <select> onchange in PHP

Post by Celauran »

Alternately, using jQuery

Code: Select all

$('select[name=oSel]').change(function() {
    $('textarea[name=oTxt]').val($(this).val());
});
Post Reply