Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically selecting modal to open
    primarykey
    data
    text
    <p>I feel like there is a super easy answer but im struggling with it. I'm using underscore and _.each to render a block of html (.datachunk and #mymodal) and iterate over an array of objects to populate data. Here is my slimmed down HTML.</p> <pre><code>&lt;!--BEGIN datachunk--&gt; &lt;div class="datachunk"&gt;&lt;/div&gt; &lt;!-- END datachunk--&gt; &lt;!--BEGIN datachunk--&gt; &lt;div class="datachunk"&gt;&lt;/div&gt; &lt;!-- END datachunk--&gt; &lt;!--BEGIN datachunk--&gt; &lt;div class="datachunk"&gt;&lt;/div&gt; &lt;!-- END datachunk--&gt; &lt;!--BEGIN Modal--&gt; &lt;div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;&lt;/div&gt; &lt;!--END Modal--&gt; &lt;!--BEGIN Modal--&gt; &lt;div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;&lt;/div&gt; &lt;!--END Modal--&gt; &lt;!--BEGIN Modal--&gt; &lt;div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;&lt;/div&gt; &lt;!--END Modal--&gt; </code></pre> <p>Here is the scenario. Click on the first .datachunk and pop open the first #mymodal. Click on the second .datachunk and pop open the second #mymodal. Click on the third .datachunk and pop open the third#mymodal. ETC...</p> <p>Right now I have:</p> <pre><code>$(".datachunk").click(function (){ $("#myModal").modal(); }); </code></pre> <p>but that only pops the first modal no matter what .datachunk I click on (as expected). </p> <p>How do I bind (not sure if thats the right term) each .datachunk with its corresponding modal? </p> <p>I can't hardcode a specific class or id because the number of .datachunks and modals is dynamic depending on how many posts are pulled in. </p> <p><em><strong></em>**Updated*<em>*</em></strong> leite you rock! My solution based of of yours:</p> <pre><code>$(".datachunk").each(function(index){ var datachunkIdentifier = "postInfo" + index; $(this).data("datachunk-class", datachunkIdentifier); $(".datachunk").eq(index).attr("identifier", datachunkIdentifier); }); $(".datachunk").each(function(index){ var datachunkIdentifier = "postInfo" + index; $(this).data("modal-id", datachunkIdentifier); $(".modal").eq(index).attr("identifier", datachunkIdentifier); }); </code></pre> <p>this adds an attr called identifier to each datachunk and modal. Now I can't figure out how to call the right modal. so clicking on div.datachunk with identifier="postInfo1" pops #myModal with identifier="postInfo1"</p> <p>new html:</p> <pre><code>&lt;div class="datachunk borderBottom" identifier="postInfo1"&gt;&lt;/div&gt; &lt;div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" identifier="postInfo1"&gt;&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload