Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery to add content ONLY in the div I am currently on
    text
    copied!<p>How do I make a selector, which makes jQuery add content ONLY in the div I am currently on? The divs are in a php foreach loop so I don't want any id's or classes for the divs.</p> <p>This is the structure of the html:</p> <pre><code>&lt;div&gt; &lt;ul class="menu-category"&gt; &lt;li&gt;Some stuff&lt;/li&gt; &lt;li class="newrow"&gt;&lt;/li&gt; &lt;!-- added when clicking on p.add --&gt; &lt;li class="edit"&gt;&lt;p class="add"&gt;Add&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div&gt; &lt;ul class="menu-category"&gt; &lt;li&gt;Some stuff&lt;/li&gt; &lt;li class="newrow"&gt;&lt;/li&gt; &lt;!-- added when clicking on p.add --&gt; &lt;li class="edit"&gt;&lt;p class="add"&gt;Add&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>This jQuery script does: When <code>&lt;p class="add"&gt;</code> is clicked -> add a row before <code>&lt;li class="edit"&gt;</code>.</p> <pre><code>// Add new row $(document).ready(function() { $('.menu-category &gt; li ul li p.add').click(function(){ $('.menu-category &gt; li ul li.edit').before("&lt;li class='newrow'&gt;&lt;h6&gt;New item&lt;/h6&gt;&lt;div&gt;&lt;p&gt;Maträtt:&lt;/p&gt;&lt;input type='text' name='name_[]' placeholder='namn' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Ingredienser:&lt;/p&gt;&lt;input type='text' name='ingredients_[]' placeholder='ingredienser' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Pris:&lt;/p&gt;&lt;input type='text' name='price_[]' placeholder='Pris' /&gt;&lt;/li&gt;"); }); }); </code></pre> <p>Problem with the script: It adds <code>&lt;li class="newrow"&gt;</code> to all divs. I want it only to happend for the div I am clicking p.add on.</p>
 

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