Meher Krishna Meher Krishna - 5 months ago 10
jQuery Question

Newly appened div class change using jquery

I want to change class of newly appended div every time. can I? How?

HTML:
<form class="form_num">
Name:<input type="text" class="name_first"/>
</form>
<span class="new_form"></span>
<button type="submit" id="submit">SUBMIT</button>


JS:

$(document).ready(function(){
$("#submit").click(function(){
$(".form_num").clone().appendTo(".new_form").attr("class","form_num2");
});
});

Answer

You can do this very easily like shown below in the demo:-

var n = 1;
$("#submit").click(function() {
     $(".form_num").clone().appendTo(".new_form").attr("class", "form_num" + ++n)
     
     // This is not needed as clone method returns the newly added form element
     //$('.new_form').find(".form_num").attr("class", "form_num" + ++n);  
           
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<form class="form_num">
  Name:
  <input type="text" class="name_first" />
</form>
<span class="new_form"></span>
<button type="submit" id="submit">SUBMIT</button>