Reputation: 2970
How to give different id to each item in @for
loop So I can use that unique ID in jQuery
to retrieve the value?
@for (int i = 0; i < searchList.Count;i++ )
{
<label for="one"><input type="checkbox" id="one"/>@searchList[i]</label>
}
Upvotes: 2
Views: 1632
Reputation: 10209
You can append the i
to the id="checkbox@i"
to make unique ids.
@for (int i = 0; i < searchList.Count;i++ )
{
<label for="checkbox@i"><input type="checkbox" id="checkbox@i" value="@searchList[i]"/>@searchList[i]</label>
}
Note: Thevalue="@searchList[i]"
attribute has been forgotten, please add it for a correct functionality.
If you want to generate the code that query the ids with jQuery
, you can make another for
like below and to make use of <text>
tag to generate the script.
@for (int i = 0; i < searchList.Count;i++ )
{
<text>
$("#checkbox@i").click(function () {
// logic
});
</text>
}
Upvotes: 1
Reputation: 172448
Try this:
@for (int i = 0; i < searchList.Count;i++ )
{
<label for="one@i"><input type="checkbox" id="one@i"/>@searchList[i]</label>
}
Upvotes: 0
Reputation: 82241
You can append the for loop index i
in id attribute:
<label for="one"><input type="checkbox" id="one@i"/>@searchList[i]</label>
Upvotes: 0
Reputation: 1040
@for (int i = 0; i < searchList.Count;i++ )
{
<label for="one"><input type="checkbox" id="one_@i"/>@searchList[i]</label>
}
Upvotes: 1