showing results for - "how to create dynamic onclick event in javascript"
Charlie
15 Jan 2017
1/* This example assumes you have two elements with an id 
2    attribute of 'cats' and 'dogs'.
3*/
4
5const conditionOneHandler = event => {
6  alert('I like cats!');
7}
8
9const conditionTwoHandler = event => {
10  alert('I like dogs!');
11}
12  
13const clickHandler = (event) => {
14
15  // Do whatever. You can use the target object
16  // from the event to conditionally handle the
17  // event.
18  switch (event.target.id) {
19    default: return;
20    case 'dogs': return conditionOneHandler(event);
21    case 'cats': return conditionTwoHandler(event);
22    
23  }
24
25} 
26
27/* 
28	Attach the click event listener to the element.
29    You can also use a comma after each line to declare
30    multiple variables of the same type, instead of 
31    writing 'const' every time. :)
32*/
33
34const dogsEl = document.querySelector('#dogs'),
35      catsEl = document.querySelector('#cats'),
36      dogsEl.onclick = event => clickHandler(event),
37      catsEl.onclick = event => clickHandler(event)
queries leading to this page
javascript click button dynamicallyadd click event on dynamically created elementjavascript dynamic button add onclickcreate button dynamically then onclick javascriptcreate dynamic element on button onclickonclick event on dynamic element javascripthow to apply click event on dynamic elementjavascript onclick dynamic elementdynamically create click events javascriptadding onclick event to javascript dynamically created elementdynamic onclick event in javascriptdynamically add onclick event javascriptdynamically created element onclickjavascript dynamic element onclickjavascript add onclick event dynamicallydynamic button javascript onclickhow to make an event with a button created dynamically javascriptjavascript dynamically created element onclickjavascript onclick dynamically addjavascript dynamic click eventjavascript set onclick to dynamic functionadd onclick javascript dynamicallyhow to add elements dynamically by onclick in javascripthandle onclick event javascript dynamically created elementjavascript dynamic button onclickonclick on dynamic buttonhow to apply click event on dynamic element in javascriptadd dynamic onclicksjavascript click event dynamic contentjavascript dynamic onclick functionhow to create dynamic onclick event in javascriptjs dynamically created button with onclickjavascript assign onclick function dynamicallyclick function on dynamically created elementjavascript click on dynamic elementadd click event to dynamic button javascripton click dynamic elementassign onclick event dynamically javascriptjavascript add onclick dynamicallydynamic button click eventdynamic button click in javascriptjavascript button onclick dynamiccreate dynamic click of button javascriptcall dynamic functions on click event in javascriptdynamic onclick function vanilla jsjavascript dynamically add onclick event to buttonjavascript add onclick function dynamicallyjs onclick dynamicaly createdhow to add onclick event dynamically in javascriptdynamically create an on click eventdynamic element javascript add onclick eventdynamic onclickonclick to dynamic elementadd onclick dynamically javascripton click for dynamic elements javascripthow to add onclick to a dynamically created button javascriptadd onclick event dynamically in javascriptonclick on dynamically created button javascripthow to add onclick event in javascript dynamically for buttonhow to dynamically add onclick event in javascriptjavascript add onclick to dynamic div 60dynamically add onclick events javascripthow to trigger onclick event in javascript after making dynamiconclick dynamic button javascriptdynamic click javascriptonclick dynamic elemnt javascripthow to create dynamic items in javascript on button clickdynamically add onclick javascripjavascript dynamically create onclick eventdynamic object in js on button clickclick on dynamically created button jsadd onclick event javascript dynamicallyhow to create dynamic onclick event in javascript