简体   繁体   中英

Change Value of Textarea on Select Option Change

I have to change the content of textarea on changing select option.for that I have wrote code as follows

                <tr><td>Template:</td><td>
                    <select t name="template" onChange = "setTemplate();">
                        <option value="0">Admission</option>
                        <option value="1">Registration</option>
                        <option value="2">Speech on 2,3 May</option>
                    </select>
                </td>
            <tr>
                <td>Message: </td>
                <td>
                <textarea name="adminmessage" id="adminmessage" rows="5" cols="50"></textarea></td>
            </tr>           
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" id="submit" value="Send SMS" class="button-primary" /></td>
            </tr>
        </table>
        </form>
<script>
            test0 = "";
            test1 = "test string 1";
            test2 = "test string 2";
            function setTemplate() {
                var otionValue = document.form.template.value;
                if (otionValue == "0") {
                  document.form.adminmessage.value = test0;
                } else if (otionValue == "1")
                  document.form.adminmessage.value = test1;
                  else if (otionValue == "2")
              document.form.textarea.value = test2;
            }; 
            </script>
</div>

but this is not works. please help me to correct.I have to do this in javascript itself not in jquery.

Your javascript syntax is not valid. Check corrected example here it here : http://jsfiddle.net/WkbLP/

javascript :

<script>
        test0 = "";
        test1 = "test string 1";
        test2 = "test string 2";
        function setTemplate(t) {
            var otionValue = t.value;
            if (otionValue == "0") {
              document.getElementById('adminmessage').innerHTML = test0;
            } else if (otionValue == "1")
              document.getElementById('adminmessage').innerHTML = test1;
              else if (otionValue == "2")
          document.getElementById('adminmessage').innerHTML = test2;
        }; 
</script>

HTML :

<select  name="template" onChange = "setTemplate(this);">
    <option value="0">Admission</option>
    <option value="1">Registration</option>
    <option value="2">Speech on 2,3 May</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM