hellogoodnight
hellogoodnight

Reputation: 2129

How use c# variable as id for div and then as argument for javascript?

I want to create a div from every object in my model, and I need to give them unique IDs for the onclick-method. How do I do this? I tried to put myid inside <%= %> as I read somewhere, it does not work.

This is my code:

@foreach(var r in Model){
   var myid = r.ToString();

   <p class="expand-one" onclick="showhide('<%= myid  %>')" onmouseover="" style="cursor: pointer;">@r.ToSection.Name</p>
               <table id=@myid class="content-one">
                   <tr>
            ......................
             ..................

Upvotes: 1

Views: 1471

Answers (2)

DevT
DevT

Reputation: 1511

Usually I use this, so you can try
<p class="expand-one" onclick="showhide('@(myid)')"></p>

Upvotes: 1

trashr0x
trashr0x

Reputation: 6565

You should be able to use Razor's @ in the same manner you use it in @r.ToSection.Name:

<p class="expand-one" onclick="showhide('@myid')"></p>

Upvotes: 3

Related Questions