Reputation: 3
<div class = "Input">
<textarea cols="10" row="10" name="add" style="resize:none" id ="text0"> </textarea>
<span class="Delete" style="visibility:hidden"> Del </span>
</div>
this is html code and
var count=0;
$('.AddText').live('click',function(e){
$('.Input:last').after($('.Input:first').clone());
});
this is JQUERY code
When i click AddText, I make another "Input" div. I want to change textarea ID like 'text' + count. How can i do it with JQUERY?
Upvotes: 0
Views: 569
Reputation: 1489
Use this piece of script. It works fine
var count=0;
$('.AddText').on('click',function(e){
$('.Input:last').after($('.Input:first').clone());
$('.Input:last textarea').attr('id','text'+((count)+1));
count=count+1;
});
Here is a DEMO
It change the textarea ID like text0
to text1
and so on.
Upvotes: 0
Reputation: 675
It is working... EXAMPLE
$('body').on('click','.AddText',function(e){
$('.Input:last').after($('.Input:first').clone());
$('.Input:last textarea').attr('id','text'+($('.Input').length-1));
});
Upvotes: 0
Reputation: 1371
You need to write like this. Dont use live anymore. Since it is deprecated already
var count=0;
$('.AddText').on('click',function(e){
$('.Input:last').after($('.Input:first').clone().find('textarea').attr('id', 'text' + count));
});
Hope it helps
Upvotes: 0
Reputation: 780798
Set the id
attribute after cloning the element.
var count=0;
$(document).on('click', '.AddText', function(e){
var newdiv = $('.Input:first').clone();
newdiv.find('textarea').attr('id', 'text'+(++count));
$('.Input:last').after(nediv);
});
But are you sure you need the textareas to have IDs? If the elements are repeated, you probably won't have any code that accesses them by ID.
Upvotes: 2