0

I'm trying to make the click event work for a list of dynamically added buttons.

See this jsfiddle: https://jsfiddle.net/jeffxiao/u1dc57za/5/

the failing line is:

$(".run").on('click', 'ul#mitigationList', clickFunction);

Any feedback is welcome.

XoXo
  • 1,560
  • 1
  • 16
  • 35
  • 1
    `$("ul#mitigationList").on('click', '.run', clickFunction);` and `$("ul#mitigationList").on('click', '.run', function(e){` – Mohamed-Yousef Apr 25 '17 at 14:03
  • i just realize the failing line should be as above. working jsfiddle: https://jsfiddle.net/jeffxiao/u1dc57za/10/ – XoXo Apr 25 '17 at 14:08

3 Answers3

1

Use this click event code :

$(document).on("click", ".run", function(){
alert("Clicked!");
});

Working jsfiddle : https://jsfiddle.net/rajnikpatel/76nmazcf/

RAJNIK PATEL
  • 1,039
  • 1
  • 17
  • 30
  • `$("ul#mitigationList").on('click', '.run', clickFunction);` works as well – XoXo Apr 25 '17 at 14:09
  • 1
    this [answer](http://stackoverflow.com/a/1207393/3380951) mentions the correct syntax: `$(staticAncestors).on(eventName, dynamicChild, function() {});` – XoXo Apr 25 '17 at 14:10
1

This issue is that $('.run').on('click', 'ul#mitigationList', clickFunction) will look for clicks on ul#mitigationList descendants of .run elements which isn't what you want.

Think of it like this:

$('elementsToWatch').on('eventName', 'descendants', functionToRun)

Have updated your fiddle:

https://jsfiddle.net/u1dc57za/11/

timothyclifford
  • 6,799
  • 7
  • 57
  • 85
0

try this:

 $("html body").on('click', '.run', clickFunction);

jsfiddle: https://jsfiddle.net/u1dc57za/12/

Sharad Kale
  • 971
  • 1
  • 7
  • 19