简体   繁体   中英

How to add Variable text to a textarea along with user entered text.

So I have some code that works except for one aspect. It currently will allow alternate text to be added to a textarea and then entering new text with it, but it clears everything in the textarea first. You are unable to start notes and then select checkboxes. You have to select the checkboxes first. I would like to be able to do ether. Here is the function:

function updatebox2()
{
    var textbox = document.getElementById("anotes");
    textbox.value = "";

    if (document.getElementById('cb5').checked) {
        textbox.value = textbox.value + "\r\nAssisted with order, Order#: ";
    }

    if (document.getElementById('cb6').checked) {
        textbox.value = textbox.value + "\r\nGave information on ";
    }

}

Here is the HTML code:

<input id="cb5" type="checkbox" value="sale" onclick="updatebox2()" />Purchase<br />
<input id="cb6" type="checkbox" value="info2" onclick="updatebox2()" />Info<br />
<input id="cb7" type="checkbox" value="callback" onclick="updatebox2()" />Will Call Back<br />
<input id="cb8" type="checkbox" value="trans" onclick="updatebox2()" />Transfer<br />
<div class="Notes" style="text-align: left; inline-block; white-space: nowrap">
    <span> Actions Taken</span>
    <a href="#" onclick="window.clipboardData.setData('Text',getValue('anotes'));">(Copy)</a><br/>
    <textarea id="anotes" rows="6" cols="50"></textarea>
</div>

You're clearing the text with textbox.value = "" every time an input is clicked and the function is called. Remove this line to stop clearing it.

If you want the user to be able to clear the text, just add a button that calls a function when clicked to clear the text.

删除行textbox.value = "";

So you want to add the text to the textarea only after the any of the checkboxes have been ticked? Is that so then:

Demo: http://jsfiddle.net/hGGsW/

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