0

I have following HTML code under Requisition.cshtml

    foreach (var item in Model.RequisitionWorks)
    {
        <tr>
            <td><div class="radio"><label name="@string.Format("Option_{0}", item.OptionNumber)">@item.OptionNumber</label></div></td>
           <td>
               <div class="radio">
                  <label>@Html.RadioButton(string.Format("Option_{0}", @item.OptionNumber), 
"0", @item.IsOptionChecked("0"), new { @class = "OptionClass", id = string.Format("Option_None_{0}", @item.ToothNumber) }) @MyModelEntities.Properties.Resource.None
    </label>
  </div>
</td>

And I generate lots of radiobuttons...

So I would like to bind some jQuery event at the moment of rendering that code.

  $("#Option_None_" + optionNumber).change(function () {
  });

I need it because I generate id of html tag on fly.

Is it possible to do?

0

2 Answers 2

3

Why not apply using the class of the option instead of an id?

$(document).ready(function(){
  $(".OptionClass").change(function () {
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to use generated on fly ID of radiobutton and don't use the CLASS.
1

You can do this by using the .on jquery method (http://api.jquery.com/on/). To accomplish this you would select your containing div and then set the onchange for the inputs within it.

$('div.radio').on('change', 'input', function() {});

Edit: it's a lot easier to do what you want to if you give the radio buttons a common class and use the above method. Generally it's not necessary use something unique like the id to attach the same event handler to each one.

2 Comments

I would like to use generated on fly ID of radiobutton and don't use the CLASS.
Why not use the class? If you need to see what was selected you can always look at the properties of the selected input via $(this) inside the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.