html - Clone element when input is filled jQuery -
so, have input asking number of moviments
<label for="nmoviments"> number of moviments: </label> <input type="text" name="nmoviments" id="nmoviments" value="" size="1" required></td>
and have div toggle form of new moviments
<div id="darktheme" class="newmov" onclick="toggle4();"> + new moviment</div>
it opens table form full of input fields.
what i'm trying function clones new moviment div it's new respective toggle form table, according number of moviments user has entered. exemple, if user types 3, should open 3 new moviments divs.
so far, have following jquery code:
$(document).ready(function(){ $("#nmoviments").change(function(){ var max= $("#nmoviments").val(); for(var i=1; i<= max; i++) { $('.newmov').clone().insertafter('newmov'); } }); });
but won't clone element, nothing happens. so, suggestions? thank much!!
your selector insertafter() should based on classname (missing ".")
also, if try call clone() in every iteration, clone entire div , clones in factorial.
you make clone of first div classname "newmov" , append it.
$(document).ready(function(){ $("#nmoviments").change(function(){ var max= $("#nmoviments").val(); for(var i=1; i<= max; i++) { $('.newmov:first').clone().insertafter('.newmov:last'); } }); });
Comments
Post a Comment