简体   繁体   中英

How to pass dynamically generated div's id to javascript in Rails

I am creating a list of buttons in Rails dynamically.

  <ul>
     <% @exams.each do |exam| %>
       <li><%= link_to exam.name, card_exam_path(:id => exam.id), :class => 'simple-button course-type', :method => :get %>
        <div class="edit_#{exam.name}">&nbsp;</div></li>
      <% end %>
 </ul>

Now I need to access the edit_#{exam.name} div in dynamically. For every dynamically generated div I need to trigger 'click' event.

<script type="text/javascript">
    *******Here I need to access all the divs seperately. How do I do that?


</script>

Instead of dynamic class create dynamic id and using live event on class write following code:

     <ul>
         <% @exams.each do |exam| %>
           <li><%= link_to exam.name, card_exam_path(:id => exam.id), :class => 'simple-button course-type', :method => :get %>
            <div class="edit_exam" id="edit_#{exam.name}">&nbsp;</div></li>
          <% end %>
     </ul>

    <script type="text/javascript">
      $('.edit_exam').live('click', function(){
        **** Here access div using $(this)

    });

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM