Solved

JavaScript: How to combine two functions.

Posted on 2014-03-03
3
494 Views
Last Modified: 2014-03-05
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:

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


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

</script>
</body>


Code that expands text:

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

<script>
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 () {
        clearTimeout(timer);
        timer = setTimeout(update, 200);

    }
}
</script>
</body>

I'd also like to add a button that call the copytoclipboard function rather than using onblur method.
0
Comment
Question by:rreiss60
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
Comment Utility
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
0
 

Author Closing Comment

by:rreiss60
Comment Utility
I was thinking that clicking a button would call the onblur method since it would remove focus from the text area.
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now