XoXo
XoXo

Reputation: 1599

bind click events to dynamic buttons

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.

Upvotes: 0

Views: 74

Answers (3)

Sharad Kale
Sharad Kale

Reputation: 971

try this:

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

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

Upvotes: 0

timothyclifford
timothyclifford

Reputation: 6959

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/

Upvotes: 1

RAJNIK PATEL
RAJNIK PATEL

Reputation: 1049

Use this click event code :

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

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

Upvotes: 1

Related Questions