Detecting RightClick->Delete in TextArea

Posted on 2009-12-26
Last Modified: 2013-11-05
I've got a textarea with a counter showing how many characters are left before you can't type anymore, and functionality truncating to a maximum size if you exceed it.

I've implemented this using onkeyup and onpaste (oninput in Opera, I'm aware of that difference), as well as oncut.

This traps everything except rightclick->delete.  How can I trigger an event based on that so I can toss the same functions at that event?

Please do not suggest jquery or other toolkits/frameworks, it's not going to happen.  I need raw javascript and HTML.

Question by:Fairlight2cx
    LVL 81

    Expert Comment

    Hello Fairlight2cx,

    Try this :

    If you past more than two chars, an alert box appear
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script language="javascript">
    	function rightClick(e) {
    		ta = document.getElementById("ta");
    		if(ta.value.length>2) {
    			alert("More than two char");
    			ta.value = ""; 
    <body >
    <textarea id="ta" cols="128" rows="16" onmousemove="rightClick()"></textarea>

    Open in new window

    LVL 7

    Accepted Solution

    Well, that's not very complete.  It also actually assumes that it should trigger the detection event as soon as you mouse over the textarea, which is just a plain fallacy in and of'd miss most pastes because it would have checked before you could hit paste from the context menu.

    I settled on a different solution, since I couldn't find an event to handle Delete.  I trigger via oncut/onpaste/oninput/onkeyup, and also have a 250ms setTimeout to check for length changes even when there are no events...which also has the side effect of letting you get status updates while someone is holding down a key with auto-repeat.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    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)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

    746 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

    12 Experts available now in Live!

    Get 1:1 Help Now