I have this function :
<script> function f(id){ return $("#" + id).val(); } </script>
And this html’s tag:
<input id=f(@payMethod) type="radio" value=" @method.Value" name="cartaCont" />
My id is buildered with a foreach that pass value to variable:
string payMethod = "payMethod"; payMethod += method.Text;
My question is: Is correct Call this index in an other function in this way ?? :
function() { $('input[id ^= #PayMethod]').attr('checked', headerChecked); }
thanks for feedback
Advertisement
Answer
It seems that you want to write some html and javascript code, relating to each item of a list.
In this case, I often prefer to directly write html and related javascript for event binding and code customization.
I supposing that method
is your razor variable in your foreach, and methods
is the enumerable variable to loop, you may would write this code for html
@foreach(var method in methods) { // this is C#, not javascript // replacing spaces to build right id code for html string payMethod = "payMethod" + method.Value.Replace(" ", ""); <text> <input id="@payMethod" class="payMethodRadio" type="radio" value="@method.Value" name="cartaCont" /> <script> $(document).ready(function () { $("#@payMethod").on('change', function (e) { /* here if you want to write some javascript events for each item related to its html ID, for example change event */ }); }); </script> </text> }
But if you want to write some common jQuery code for this set of radio buttons, I’ prefer to manage it with class selector rather than ID rules, as follow
function() { $('payMethodRadio')... }
Hope this, help you for better code management.
Br Andryx