Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Id & Class Inside a variable containing html code jquery

enter image description here

What I'm Trying to Do is On click of AddMore Button I'm Getting id of the button

 <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 have kept this div and what I'm On click is

$(".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 append id to the id and the class

Error:

adddiv.find is not function
like image 897
Arijit Mukherjee Avatar asked Dec 24 '15 10:12

Arijit Mukherjee


2 Answers

That is beacause adddiv is html string and not jquery object of element. use:

var adddiv = $('#MemberCountDiv');
alert(adddiv.html());
adddiv.find('.MemberDescriptionAdd').attr('id', 'your-new-id');
alert(adddiv.html());
like image 153
Milind Anantwar Avatar answered Sep 24 '22 06:09

Milind Anantwar


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

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

Now adddiv acts as a HTML Element and you can make all the changes.

like image 42
Praveen Kumar Purushothaman Avatar answered Sep 21 '22 06:09

Praveen Kumar Purushothaman