Change id and class inside variable containing jquery html code

enter image description here

What I'm trying to do is click the AddMore button. I get the button id

<div id="MemberCountDiv"> <div class="MemberDescriptionAdd" style="display:none;"> <select name="MemberCountType" id="MemberCountTypeX" class="MemberCountTypeX form-control "> <option selected="selected" value="">Condition</option> <option value="<">Less Than (&le;)</option> <option value=">">Greater Than (&ge;)</option> <option value="=">Equal</option> </select> <select name="MemberCount" id="MemberCountX" class="MemberCountX form-control "> <option selected="selected" value="">Members</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="8">7 Or More Than 7</option> </select> <button id="RemoveX" class="btn btn-blue" type="button">Remove</button> </div> </div> 

I saved this div and that I am on click

 $(".AddMoreButton").click(function () { var type = $(this).val(); var idname = $(this).attr("id"); var id = idname.replace("AddMore", ""); alert(id); var adddiv = $('#MemberCountDiv').html(); alert(adddiv); adddiv.find('.MemberDescriptionAdd').attr('id', 'your-new-id'); alert(adddiv); }); 

What I'm trying to do is add id to id and class

Error:

 adddiv.find is not function 
+6
source share
2 answers

This beacause adddiv is an html string, not an element jquery object. Application:

 var adddiv = $('#MemberCountDiv'); alert(adddiv.html()); adddiv.find('.MemberDescriptionAdd').attr('id', 'your-new-id'); alert(adddiv.html()); 
+5
source

You cannot do this using a string. Just use .clone() :

 var adddiv = $('#MemberCountDiv').clone(); alert(adddiv.html()); adddiv.find('.MemberDescriptionAdd').attr('id', 'your-new-id'); alert(adddiv.html()); 

adddiv now acts as an HTML element and you can make all the changes.

+2
source

All Articles