Rob M
Rob M

Reputation: 1031

dynamically added radio buttons change not detected jquery

I have a group of radio buttons being created while the page is loading, and I have attached a listener to them using on(), but the when I change the selected radio button, nothing happens.

Html:

<input type="radio" name="condition" id="cond_1" value="1">
<input type="radio" name="condition" id="cond_2" value="2">

jquery:

$("input[name=condition]:radio").on("change",function(){...});

This has worked in the past, but for some reason, it doesn't appear to be working now. Any thoughts?

Upvotes: 1

Views: 2773

Answers (1)

joellustigman
joellustigman

Reputation: 336

Bind on to the body, or any closer ancestor element that doesn't change:

$("body").on("change", "input[name=condition]:radio", function(){...});

Upvotes: 4

Related Questions