Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript: How to combine two functions.

Posted on 2014-03-03
3
Medium Priority
?
640 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 1500 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

Industry Leaders: 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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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…
The viewer will learn how to count occurrences of each item in an array.

670 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