Skip to content Skip to sidebar Skip to footer

Loading Text Into Textarea Based On Drop Down Selection

To start, I have searched the site and found this: How to fill in a text field with drop down selection but it did not suit my needs, as I'm generating very long templates and the

Solution 1:

Okay you had many problems in code the major one was specifying onchange in option instead of selects, there also little problem which I solved and here is a working example http://jsfiddle.net/gKsdK/1/

Here is the markup

<h1>Note Generator</h1><divclass="left">
CSTemplates
<p><selectclass="c10"onchange="showCSTemplates(this);"><optionselected="selected"value=""id="Templates">Please select a template...</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select></p><p><textareacols="30"rows="20"readonly="readonly"id="CSTemplates">
Templates will auto-populate 
here depending on the 
selection made from the 

[CSTemplates] 

drop-down list.
</textarea></p></div><!--left ends here-->

Here is the JS

functionshowCSTemplates(sel){   

locations =[ "", /*this remains blank for first selection in drop-down list*//*option 1*/" This is template 1 that  will appear in a textarea keeping its formatting as  is. ",

/*option 2*/" This is template 2 that  will appear in a textarea keeping its  formatting as  is. Credentials:  Contact Info: ",

/*option 3*/" This is template 3 that  will appear in a textarea keeping its formatting as  is. Donec tortor lorem,  ornare vitae commodo nec,  sagittis et nunc. Maecenas sagittis quam ",

/*option 4*/"etc",

/*option 5*/"etc...", ];
                   srcLocation = locations    [sel.selectedIndex];        
   if (srcLocation != undefined && srcLocation != "") {      
                  document.getElementById('CSTemplates').innerHTML= srcLocation;   
 } 
}

Solution 2:

You have bound the onchange event on option instead of select.

Solution 3:

If you want to have a string on several lines, you have to concatenate it:

/*option 1*/" This is template 1 that  will appear in a \n"+"textarea keeping \n"+"its formatting \n"+"as \n"+"is. \n",

The \n are here only to create the breaklines into your .

And if you want to change the contents of a textarea, use the property .value and not .innerHTML !

Post a Comment for "Loading Text Into Textarea Based On Drop Down Selection"