0

I'm new at jQuery. I'm cloning one specific HTML block and trying to change last cloned html's element's nameor id, class but i couldn't. I can find and alert name of element that i want to change, but i can not change it. Hope you can help me.. thanks in advance.

My code

jQuery(document).on("click",".smclass",function(){

var html = jQuery(this).closest(".entry-edit").clone().appendTo(".main-col-inner").html();
html = jQuery(html).find('.start').attr('name','optional[2][type]').attr('id', 'Type1');

 }

HTML

<div class="entry-edit">

<!-- html codes -->

<button id="Addfield" title="Field Ekle" type="button" class="smclass" onclick="" style="float: right;"><span><span><span>Field Ekle</span></span></span></button>

<!-- html codes -->

<select id="Type" name="optional[1][type]" class="start">
    <option value="0">Date</option>
    <option value="1">Text</option>
    <option value="2">Select</option>
</select>

<!-- html codes -->
</div>

2 Answers 2

3

Your code is just putting an html string into a variable and then creating a new jQuery object with said html. You are not selecting the new element and changing it

//Just remove the .html() call and you will have the cloned element
var ele = jQuery(this).closest(".entry-edit").clone().appendTo(".main-col-inner");
ele.find('.start').attr('name','optional[2][type]').attr('id', 'Type1');

You also do not need to make multiple .attr calls, you can pass it an object with the name value pairs you want to set

ele.find(".start").attr({
   "name":"optional[2][type]",
   "id":"Type1"
});
Sign up to request clarification or add additional context in comments.

Comments

-1

just try vise versa

var clonedObject = jQuery(this).closest(".entry-edit").clone();
clonedObject .find('.start').attr('name','optional[2][type]').attr('id', 'Type1');
clonedObject.appendTo(".main-col-inner").html();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.