Using JQuery with dynamically added controls (MVC)

Posted by Mario on Stack Overflow See other posts from Stack Overflow or by Mario
Published on 2010-03-29T17:42:32Z Indexed on 2010/03/29 17:53 UTC
Read the original article Hit count: 287

Filed under:
|

I have a set of radio buttons that are dynamically generated (via JSON call):

StringBuilder sbEligAll = new StringBuilder();

sbEligAll.Append("<div id='currentselectiondepartment'>");
sbEligAll.Append("  <input type='radio' name='selectalldepartment' id='radCurrent' value='0' /><label for='radCurrent'>Current</label>");
sbEligAll.Append("  <input type='radio' name='selectalldepartment' id='radFuture' value='1' /><label for='radFuture'>Future</label>");
sbEligAll.Append("  <input type='radio' name='selectalldepartment' id='radCurrentAll' value='2' /><label for='radCurrentAll'>Current All</label>");
sbEligAll.Append("  <input type='radio' name='selectalldepartment' id='radFutureAll' value='3' /><label for='radFutureAll'>Future All</label>");
sbEligAll.Append("</div>");

Then:

return Json(new { TableList = sbElig.ToString() }, JsonRequestBehavior.AllowGet);

Is there a way to get jQuery to be able to interact with these?

For example:

$("input[name=selectalldepartment]").change(function() {
     var str = $('input[name=selectalldepartment]:checked').val();
     $(".radiolist[value='" + str + "']").attr("checked", true);
});

I've tried everything I know how to do, but the jQuery just doesnt fire. (I've added alerts to the jQuery code to see if it actually gets called, and it does not).

Any thoughts?!

Thanks!

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery