JavaScript: How to combine two functions.

rreiss60 used Ask the Experts™
I have found two JavaScript functions that I would like to use together.  I don't know how.

The idea is to use the text expansion function to expand abbreviations into full text. Next, the expanded text in the textarea is copied to the clipboard.

Here is what I have:

Code to copy to clipboard:

    <textarea rows="25" cols="75" wrap="hard" onblur="CopyToClipboard(this)">

<script language="JavaScript">
function CopyToClipboard(text) {
    Copied = text.createTextRange();


Code that expands text:

<textarea id="comment" rows="20" cols="300"></textarea>

shortcuts = {
    "cci": "customer called in",
    "rfc": "request for comments",
    "www": "world wide web"

window.onload = function () {
    var ta = document.getElementById("comment");
    var timer = 0;
    var re = new RegExp("\\b(" + Object.keys(shortcuts).join("|") + ")\\b", "g");

    update = function () {
        ta.value = ta.value.replace(re, function ($0, $1) {
            return shortcuts[$1.toLowerCase()];

    ta.onkeydown = function () {
        timer = setTimeout(update, 200);


I'd also like to add a button that call the copytoclipboard function rather than using onblur method.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
in order to combine, you can always call CopyToClipboard(ta.value) inside your update() function

in your posted code, your onblur should be CopyToClipboard(this.value) and not (this)

your button could be something like
<input type="button" value="copy to clipboard" onclick="document.getElementById('comment').value" />

if you do the combination, the button is quite useless since the text will already be in the clipboard anyway, unless the user did not type anything in the field


I was thinking that clicking a button would call the onblur method since it would remove focus from the text area.
yes. if the textarea was focused before the button is clicked, the onblur method of the textarea will fire first and the onclick method of the button afterwards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial