HelpASisterOut HelpASisterOut - 4 months ago 27
C# Question

how to Javascript toggle ID of div generated on runtime

I am sending a Model to a View in MVC,

and for each "Person" in my Model, I want to create a button that says "Show Details", when the user clicks it, a div with more information about that person toggles.

I am generating a

a href
id assigned to each person ID like the following:

foreach (var person in Model.Persons)
<a href="#" id="@Html.Raw("showDetails-" +">Show Details</a>
<div id="@Html.Raw("detailsDiv-" +">
<!--Content Here -->


I want to create the javascript that toggles on a click event

So I want to write this script but with the unique id for each person:

I want to get the id of the div that needs to be toggled.

I tried writing the script inside the foreach loop so I can be able to read the value, but it didnt work:

$(document).ready(function () {
$("").click(function () {

Can anyone help?


put a class to your links like class="showDetails".

Now you Can create a global Event for all that links:

$('.showDetails').click(function(e) {
   var id = // extract id.. split with '-' or whatever..
   $('#details-' + id).toggle();