Solved

JavaScript: How to combine two functions.

Posted on 2014-03-03
3
541 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 27

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39901788
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
ID: 39903241
I was thinking that clicking a button would call the onblur method since it would remove focus from the text area.
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39906259
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

828 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