javascript - How to append div for each div -


this question has answer here:

i have list want append each div when click in "add". works every time click on "add" works of divs. want attend new div each div click. here code:

$(".add").click(function() {    $(".content").append('<ul class="pricepart"><li><input type="text" /></li></ul>');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="content">    <ul class="pricepart">      <li>        <input type="text" />        <span class="add">add+</span>      </li>    </ul>  </div>  <div class="content">    <ul class="pricepart">      <li>        <input type="text">        <span class="add">add+</span>      </li>    </ul>  </div>

because applying method class , ever find class function executed.

you need define function should executed on parent class of click element.

you can use .parents() or .closest() selector same this

$(".add").click(function () {   $(this).closest(".content").append('<ul class="pricepart"><li><input type="text" /></li></ul>'); }); 

or

$(".add").click(function () {   $(this).parents(".content").append('<ul class="pricepart"><li><input type="text" /></li></ul>'); }); 

sometime .parents() creates issue if there multilevel inheritance same classes in case work fine


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -