Search code examples
javascripttransfer

Javascript function needs retouching


            function CopyValues(oDDL, sTargetId) {
                var arrValues = new Array();
                for (var i = 0; i < oDDL.options.length; i++) {
                    var curOption = oDDL.options[i];
                    if (curOption.selected)
                        arrValues.push(curOption.value);
                }
                document.getElementById(sTargetId).value += arrValues.join("\n");
            }

This javascript function works awesomely, it was provided in another question I asked months ago... This function transfers the value from a select box to the element id of my choice. I use the function like this:

onclick="CopyValues(this, 'subject');"

All I want now is that this value should be added IN THE BEGINNING of any other text in my textarea or input[type="text"]. Any ideas on how to change this to that? I can't do javascript myself.


Solution

  • Change

    document.getElementById(sTargetId).value += arrValues.join("\n");
    

    to

    var targetElement = document.getElementById(sTargetId);
    targetElement.value = arrValues.join("\n") + targetElement.value;