Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  84] [ 2]  / answers: 1 / hits: 16776  / 9 Years ago, fri, october 30, 2015, 12:00:00

I'm doing a clone of a DIV element on button click, I'm able to change the value of ID of the DIV element I'm cloning. But is it possible to change the id of the inner element.



In the below code I'm changing the Id of #selection while cloning, I need to dynamically change the id #select.



<div id=selections>
<div class=input-group id=selection>
<span class=input-group-addon>
<i class=icon wb-menu aria-hidden=true></i>
</span>
<select class=show-tick data-plugin=select2 id=select>
<option>True</option>
<option>False</option>
</select>
</div>
</div>
<button class=btn btn-primary type=button style=margin-left: 30px;>
Add new selection
</button>


The JS below



$(function() {
//on click
$(body).on(click, .btn-primary, function() {
alert($(.input-group).length)
var
//get length of selections
length = $(.input-group).length,
//create new id
newId = selection- + length++,
//clone first element with new id
clone = $(#selection).clone().attr(id, newId);
//append clone on the end
$(#selections).append(clone);
});
});

More From » jquery

 Answers
41

Yes.. its totally possible as follows:



var clone = $(#selection).clone();
clone.attr(id, newId);

clone.find(#select).attr(id,select-+length);

//append clone on the end
$(#selections).append(clone);

[#64556] Tuesday, October 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zackeryzainv

Total Points: 61
Total Questions: 102
Total Answers: 99

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;