2

Is it possible to include div tags within a for loop in an asp.net page and assign its id dynamically so that I can refer a particular div tag with the assigned id in a jquery script. I want to do something like below.

@for(var i = 0; i < projectCountGlobal; i++) 
{
    <div id="i" > // id should be assigned dynamically
        <div id="ProjectImage">

        </div>

        <div id="ProjectName">

        </div>


        <div id="Experts">
           <div id="ExpertImage">

           </div>
           <div id="ExpertName">

           </div>
        </div>
     </div>
}
0

2 Answers 2

2

From the snippet it seems like you are doing razor view so you can simply do like this,

    <div id="@i" > 

that shall do the trick.

4
  • Thanks Furqan that did the trick.. Further is it possible to refer the inner div ids, for an example ProjectName div tag by 'ProjectName{@i}'? Feb 6, 2013 at 11:18
  • Yes it is possible the same way, this is the beauty of razor to mix html and server side code. "ProjectName@i" Feb 6, 2013 at 11:20
  • Hey I tried to access inner div 'Projectname@i" in the jquery code by $('#ProjectName'+i).html(); But it didn't work for me. Feb 6, 2013 at 12:52
  • @Rameshi, did u check that project divs are created with concatenated values of 'i'? Feb 6, 2013 at 13:22
0

i you can do this using following code snippet:

  $('.topopup').hover(function () {           
      for (var i = 0; i < 5; i++) {
          $('body').append('<div id=' + i + '>a</div>');
       }           
  })

// HTML code on page

<body>
   <a href="#" class="topopup">Click Here </a>
   <div id="toPopup" style="display:none;">
   <div id="popup_content">Rahul Deshmukh</div>
   <div id="backgroundPopup"></div>
   <div id="containerDiv"></div>
   </div>
</body>

here i am appending div inside body tag for this you need to add min.js also in your peoject folder here "topopup" is a anchor tag and on the mouse over of this anchor tag, i am adding div inside body tag.

2
  • Rahul, it seems exactly what I was looking for. But this ddn't work for me even I added the min.js to my project folder. Feb 6, 2013 at 12:47
  • what problem you are facing? Feb 7, 2013 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.