Solved

JavaScript: How to combine two functions.

Posted on 2014-03-03
3
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

752 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