How to use javascript onmouseLeave and OnContextMenu Event

Many Programmers do not have an idea when and how to Use onMouseLeave Event and when it will be Fired.My Article is for those programmers to give brief idea How to use these events and as well as when to use.

In some Applictation/Software when you work with text area Controls.You need to count the number of characters entered in the Text-box area. Some Users will type the characters in text box and some users will Ctrl-c + ctrl + v (copy and paste with keyboard controls).But some Users will right click the Mouse on the text area then paste the texts then you should able to see the Number of Characters Count present in the text area. Those who wants right click functionality in their Application/Software.This article will be useful for them.

The below Snippet of code and Picture will show you using oncontextMenu on textArea.


function fnShowCount(field,field2,maxlimit)
var countfield = document.getElementById(txtMsg);
if ( field.value.length > maxlimit ) {
field.value = field.value.substring( 0, maxlimit );
return false;
} else {
countfield.value = maxlimit - field.value.length;

textarea name="txtMsg" id="txtMsg" runat="server" onkeyup="fnShowCount();"

See the below Picture will Show you the how the Output will be without Using on Mouse Leave Event


See the Number of characters remain same.i.e 0(Zero....).Because the Event had not Fired.

After Using onMouseleave event see the Output

textarea name="txtMsg" id="txtMsg" runat="server" onkeyup="fnShowCount();"
oncontextmenu="fnShowCount();" onmouseLeave="fnShowCount();"


After Immediately leaving the Mouse button the Event will fire and with immediate effect it will show the Characters Count


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: