Chuyển đến nội dung chính

Types of calling a function while click in javascript

     In javascript we can call a function while clicking html elements in three types, they are define in following example.

1) onclick
2) object.onclick
3) addEventListener


1) onclick:
     This is type is define inside the html tag. When you want to call a function in while clicking you can use the onclick attribute inside the html elements.

Syntax

<element onclick="script or function calling">


Example

<script>

 function cl_fun()

{

  alert("Function Called");

}

</script>



 <input type="button" onclick="cl_fun();" value="Click Here">


2) object.onclick
     In this method we are call a function using html elements id.

Syntax

var obj=document.getElementById("element_id");

obj.onclick=function(){/*script here*/};



Example

<script>

 var obj=document.getElementById("btnid");

 obj.onclick=function(){alert("Function Called");};



or

 var obj=document.getElementById("btnid");

 obj.onclick=function_name;

</script>



  <input type="button" id="btnid" value="Click Here">


3) addEventListener
     addEventListener also define such as object.onclick, the example is shown below.

Syntax:

 var obj=document.getElementById("element_id");

obj.addEventListener("click",function(){/*script here*/},useCapture);



 or

 var obj=document.getElementById("element_id");

 obj.addEventListener("click",function_name,useCapture);


addEventListener false why?
       The third parameter is If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTargets beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events for a detailed explanation.

Example

<script>

 var obj=document.getElementById("btnid");

 obj.addEventListener("click",function(){alert("Function Called"),false};

</script>



 <input type="button" id="btnid" value="Click Here">


Example Program:- (Editor)


Editor is Loading...

Advertisement




Tag:
click function using javascrip in allinworld99, click event in javascript allinworld99

Nhận xét

Popular Posts

CCleaner 5.29 Pro full registration key for All Version

Download from Official Site: http://www.piriform.com/ccleaner/download/professional  Disconnect Internet Before Registration Don't Update Automatically. Download 5.29 Pro Full Version Previous Version Download here More Full Version Software: InternetDownload Manager (IDM) 6.25.20 iCare Data Recovery v5.1 Full with Registration Xilisoft Video Converter Ultimate 7.7.3.20131014 FuLL AdvancedSystemCare 8.1 Pro full Download Tuneup Utility 2014 Product key Driver Booster License Code Get Camtasia Studio full version free and save $300!!! CCleaner Professional 5.18 full with Registration Key Mini Tool Power Data recovery and Partition Wizard full with registration

Adobe Photoshop 7.0 Free Download Full Version With Key

Some Screenshots of Adobe Photoshop 7.0 Free Download Adobe Photoshop 7.0 With Keys Full Version! FileSize: 154 MB Download

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064)

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064) is a software for creating PDF files. With the help of this software, the user can convert all text files, photos and other relevant documents to PDF format. Adobe Acrobat software is popular with many software users due to the compatibility with most software. You can plan it in the printer's configuration area by typing Ctrl + P keys that are specific to print operations, print texts or anything printable in Adobe Acrobat as a PDF. Adobe Acrobat software features Ability to convert to other formats like images and Word Scan directly from the scanner The presence of internal OCR to identify texts Optimized mode for displaying documents as much as possible Specific compatibility with high-quality images to showcase the best of the documents Ability to do things in a batch like conversion Ability to add new actions to PDF documents Ability to interconnect multiple PDF documents Ability to build form System Requirements Operating Sys...