admin管理员组

文章数量:1312662

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

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
Share Improve this question edited Dec 24, 2015 at 20:33 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges260 bronze badges asked Dec 24, 2015 at 10:30 Arijit MukherjeeArijit Mukherjee 3,8852 gold badges36 silver badges53 bronze badges 1
  • 3 Try $('#MemberCountDiv').find('.MemberDescriptionAdd').attr('id', 'your-new-id'); – Senjuti Mahapatra Commented Dec 24, 2015 at 10:33
Add a ment  | 

2 Answers 2

Reset to default 6

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());

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.

本文标签: javascriptChange Id amp Class Inside a variable containing html code jqueryStack Overflow