Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q659 How can I put the value of a selected select option into a textarea and retain the line breaks specified with \r\n?

You are here: irt.org | FAQ | JavaScript | Form | 10.1 | Q659 [ previous next ]

Try:

<SCRIPT LANGUAGE="JavaScript"><!--
function replace(string,text,by) {
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

document.write(replace('XXXX','X','Y') + '<BR>');}
//--></SCRIPT>

<FORM NAME="myForm">
<TEXTAREA COLS="10" NAME="myText" ROWS="3"></TEXTAREA>
<SELECT NAME="mySelect" onChange="document.myForm.myText.value=replace(document.myForm.mySelect.options[document.myForm.mySelect.selectedIndex].value,'\\r\\n','\r\n')">
<OPTION>Select:
<OPTION VALUE="Check\r\nthis\r\nout">Select me!
</SELECT>
</FORM>

©2018 Martin Webb