Currently, I am passing a list of object from controller to a view, and generate labels by the object's name.
What I am trying to do is to generate a jQuery function that will Dynamically create functions (toggle a form with relative lable id) for each label after being clicked.
The jQuery function is not working, I could not output the corrent jQuery function in the webpage...... Could you give me soem hints?
<table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.Label(item.productName, new { @id = item.productId})
</td>
</tr>
}
</table>
<script type="text/javascript">
$(document).ready(function () {
@foreach (var item in Model)
{
$(@item.productId).click)(function({
//do something
}));
}
});
</script>
Thanks very much!
$('@item.productId').on('click', function() {});is more what you want, however a cheaper and faster way is to give all those labels a class name, and only have one click handler; ie$('.product_lbl').on('click', function() { /* Do something for whichever label was clicked */ });$('#idGoesHere')would help.