Tuesday, February 17, 2009

Important javascript Event List & Description




Please visit my new Web Site https://coderstechzone.com



Event?
Event means action. When user click a button means user wants to perform an action. Events like performing:

  1. Click on a button
  2. Press a key
  3. Highlight text
  4. Drag & Drop

List of available javascript Events are given below:
*To see the example scroll down
EventDescriptionApplicable for ?
onAbortWhen user cancel image loadingImage
onBlurWhen control lost its FocusButton, Checkbox, FileUpload, Layer, Password, Radio, Reset, Select, Submit, Text, TextArea, Window
onChangeCombo index change/Textbox data changeText, TextArea, Select
onClickMost useful event. Occur when user click on a buttonButton, Checkbox, Link, Radio, Submit
onDblClickWhen user double click on a LinkLink
onFocusWhen an object get focusButton, Checkbox, Radio, Reset, Select, Submit, Text, TextArea
onKeyDownWhen the key is down means pressing timeLink, TextArea
onKeyPressWhen user presses/holds a keyLink, TextArea
onKeyUpWhen user release a keyLink, TextArea
onLoadAfter completion of loading a pageWindow, Image
onMouseDownWhen mouse pressesLink, Button, Document
onMouseMoveWhen user moves the mouseLink, Button, Document
onMouseOutWhen user moves the mouse away from a objectImage, Link
onMouseOverWhen user moves the mouse over a objectImage, Link
onMouseUpWhen user release mouseDocument, Button, Link
onMoveWhen user moves the browser window or a frameDocument, Button, Link
onResetBy clicking on reset ButtonForm
onResizeWhen user resizes the browser window or a frameWindow
onSelectWhen user select textText, TextArea
onSubmitWhen user clicks the submit buttonForm
onUnloadWhen user leaves the page/close the browserWindow

Example onClick Event:
Here i am showing how one can use this event & add event handler. Example covers both asp.net server side control & HTML control. To run this example just copy the below code then run your page. Add the below function in your javascript block:
function funcOnClick()
{
// Code goes here
alert("Click event fired");
// Return false never make a post back to server.
// so one can add validation by using this concept. If return true then the page will be post back to server
return false;
}

Add the handler:
<asp:button runat="server" id="cmdOnClick" text="OnClientClick" onclientclick="return funcOnClick();">
<asp:button runat="server" id="cmdOnClick" text="OnClientClick" onclientclick="return funcOnClick();">
<asp:button runat="server" id="cmdRunTime" text="Server Control onClick">
<input type="button" name="cmdHTML" value="HTML onClick" onclick="return funcOnClick();">

For cmdRunTime here i am showing how one can bind event handler in runtime. Here i am using asp.net(c#). Add the below line under Page_Load Event:
cmdRunTime.Attributes.Add("onClick", "return funcOnClick();");

Live Demo:
----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

** Click to view the practical example on onMouseOver & onMouseOut
** Click to view the practical example on onclick
** Click to view the practical example on onkeypress
** Click to view the practicle example on onunload
** Click to view the practical example on OnClientClick

Ref:
http://www.w3schools.com/jsref/jsref_events.asp

6 comments:

Nilesh Thakkar said...

@Shawpnendu
Hi,
Nice Blog!!

As you said
Here i am using asp.net(c#). Add the below line under Page_Load Event:

cmdRunTime.Attributes.Add("onClick", "return funcOnClick();");

It may create problem, because in C# onClick event is reserved for server side event. You should suggest OnClientClick instead. Let me know if i am wrong. Thanks and keep Posting!! :)

Saion Roy said...

HI
Thanks for your comment.
Did you try it? :)

Anonymous said...

Hi Bikash,

Nice one.

I have a web page where i have 1 text box and 3 button fields in which 2 buttons to redirect to someother pages and 1 button is a search click.

When i enter something in the text box and click enter(return click) Its taking to me to other page (ie it is not performing serch click rather cliking one of the other two buttons)

could you please help me in this.

I tried setting tabindex to all the web controls but no luck.

Thanks for help in advance

Saion Roy said...

Hi Anonymous,
Sorry for my late reply. First of all when you press enter then the immediate next control will be clicked. This is the common scenario. To overcome this problem use javascript.

Add keypress event handler & check the keycode. If keycode=13(Means enter) then programatically click on your required button to fire. In your case click on search button.

Let me know if you have further concern.

Lavan said...

Thanks Bikash,

when we click enter next control will be clicked, true, but my page is not working as designed.

I even set the tab index to make it work but no use. I thought javascript would help and researched a lotfor the javascript code but some how found an easy solution than that. ie
form id="form1" runat ="server" defaultbutton="buttonid"

Any way thanks for your reply Bikash

Saion Roy said...

Hi Lavan,
Thanks to correct me. I thought that anonymous asked for classic asp since we are familiar of such type of question from classic ASP developers.

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash