jquery clone() dont do anything












2















i have a simple code to clone dropdown menu with div :



<div id="tag-wrapper">
<label for="">Tag (max 3)</label>
<button id="add_tag">Add Tag</button>
{{-- <div id="add_tag">Add Tag</div> --}}

<select name="tags" id="tag_select">
<option value="0">Nothing</option>
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
$(document).ready(function(){
$("#add_tag").click(function(e){
e.preventDefault();
counter++;
if(counter < 3){
$("#tag_select").clone().appendTo('#tag_wrapper');
}
});
});
</script>
</div>


when the button with id=add_tag clicked, it should clone the dropdown menu with id=tag_select, but that not working. Any suggestion?










share|improve this question


















  • 1





    It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

    – Diamant
    Nov 24 '18 at 14:16













  • Trying to be used e.preventDefault(); at the bottom of your jquery code

    – MD. Jubair Mizan
    Nov 24 '18 at 14:23











  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:26
















2















i have a simple code to clone dropdown menu with div :



<div id="tag-wrapper">
<label for="">Tag (max 3)</label>
<button id="add_tag">Add Tag</button>
{{-- <div id="add_tag">Add Tag</div> --}}

<select name="tags" id="tag_select">
<option value="0">Nothing</option>
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
$(document).ready(function(){
$("#add_tag").click(function(e){
e.preventDefault();
counter++;
if(counter < 3){
$("#tag_select").clone().appendTo('#tag_wrapper');
}
});
});
</script>
</div>


when the button with id=add_tag clicked, it should clone the dropdown menu with id=tag_select, but that not working. Any suggestion?










share|improve this question


















  • 1





    It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

    – Diamant
    Nov 24 '18 at 14:16













  • Trying to be used e.preventDefault(); at the bottom of your jquery code

    – MD. Jubair Mizan
    Nov 24 '18 at 14:23











  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:26














2












2








2








i have a simple code to clone dropdown menu with div :



<div id="tag-wrapper">
<label for="">Tag (max 3)</label>
<button id="add_tag">Add Tag</button>
{{-- <div id="add_tag">Add Tag</div> --}}

<select name="tags" id="tag_select">
<option value="0">Nothing</option>
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
$(document).ready(function(){
$("#add_tag").click(function(e){
e.preventDefault();
counter++;
if(counter < 3){
$("#tag_select").clone().appendTo('#tag_wrapper');
}
});
});
</script>
</div>


when the button with id=add_tag clicked, it should clone the dropdown menu with id=tag_select, but that not working. Any suggestion?










share|improve this question














i have a simple code to clone dropdown menu with div :



<div id="tag-wrapper">
<label for="">Tag (max 3)</label>
<button id="add_tag">Add Tag</button>
{{-- <div id="add_tag">Add Tag</div> --}}

<select name="tags" id="tag_select">
<option value="0">Nothing</option>
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
$(document).ready(function(){
$("#add_tag").click(function(e){
e.preventDefault();
counter++;
if(counter < 3){
$("#tag_select").clone().appendTo('#tag_wrapper');
}
});
});
</script>
</div>


when the button with id=add_tag clicked, it should clone the dropdown menu with id=tag_select, but that not working. Any suggestion?







jquery laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 14:09









Granite BagasGranite Bagas

162




162








  • 1





    It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

    – Diamant
    Nov 24 '18 at 14:16













  • Trying to be used e.preventDefault(); at the bottom of your jquery code

    – MD. Jubair Mizan
    Nov 24 '18 at 14:23











  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:26














  • 1





    It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

    – Diamant
    Nov 24 '18 at 14:16













  • Trying to be used e.preventDefault(); at the bottom of your jquery code

    – MD. Jubair Mizan
    Nov 24 '18 at 14:23











  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:26








1




1





It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

– Diamant
Nov 24 '18 at 14:16







It should be $("#add_tag").on('click', function() { ... }); click() function is triggered once when the document is ready.

– Diamant
Nov 24 '18 at 14:16















Trying to be used e.preventDefault(); at the bottom of your jquery code

– MD. Jubair Mizan
Nov 24 '18 at 14:23





Trying to be used e.preventDefault(); at the bottom of your jquery code

– MD. Jubair Mizan
Nov 24 '18 at 14:23













thank you, it fixed

– Granite Bagas
Nov 25 '18 at 15:26





thank you, it fixed

– Granite Bagas
Nov 25 '18 at 15:26












1 Answer
1






active

oldest

votes


















0














The real issue is your wrapper id is tag-wrapper. But you miss spelled while appending as tag_wrapper. Even though you fix this, there are 2 more issues will raise.





  1. Since you are cloning the element, you cant use id attribute any more. Give it a class name as ,




    Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. - https://api.jquery.com/clone/




    <select name="tags" class="tag_select">



  2. Now, while cloning, it will select all the elements with the same class name and result in duplicate elements. You can avoid this by retrieving only the first/last element from the cloned ones.



    <div id="tag-wrapper">
    <label for="">Tag (max 3)</label>
    <button id="add_tag">Add Tag</button>

    <div id="tag_selects">
    <select name="tags" class="tag_select">
    <option value="0">Nothing</option>
    @foreach ($tags as $tag)
    <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
    </select>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var counter = 0;
    $(document).ready(function(){
    $("#add_tag").click(function(e){
    e.preventDefault();
    counter++;
    if(counter < 3){
    $(".tag_select").clone().last().appendTo('#tag_selects');
    }
    });
    });
    </script>
    </div>



JS Fiddle working example : https://jsfiddle.net/za8h2L19/1/






share|improve this answer
























  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:27











  • You can accept the answer then :)

    – Ijas Ameenudeen
    Nov 25 '18 at 15:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458992%2fjquery-clone-dont-do-anything%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The real issue is your wrapper id is tag-wrapper. But you miss spelled while appending as tag_wrapper. Even though you fix this, there are 2 more issues will raise.





  1. Since you are cloning the element, you cant use id attribute any more. Give it a class name as ,




    Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. - https://api.jquery.com/clone/




    <select name="tags" class="tag_select">



  2. Now, while cloning, it will select all the elements with the same class name and result in duplicate elements. You can avoid this by retrieving only the first/last element from the cloned ones.



    <div id="tag-wrapper">
    <label for="">Tag (max 3)</label>
    <button id="add_tag">Add Tag</button>

    <div id="tag_selects">
    <select name="tags" class="tag_select">
    <option value="0">Nothing</option>
    @foreach ($tags as $tag)
    <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
    </select>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var counter = 0;
    $(document).ready(function(){
    $("#add_tag").click(function(e){
    e.preventDefault();
    counter++;
    if(counter < 3){
    $(".tag_select").clone().last().appendTo('#tag_selects');
    }
    });
    });
    </script>
    </div>



JS Fiddle working example : https://jsfiddle.net/za8h2L19/1/






share|improve this answer
























  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:27











  • You can accept the answer then :)

    – Ijas Ameenudeen
    Nov 25 '18 at 15:29
















0














The real issue is your wrapper id is tag-wrapper. But you miss spelled while appending as tag_wrapper. Even though you fix this, there are 2 more issues will raise.





  1. Since you are cloning the element, you cant use id attribute any more. Give it a class name as ,




    Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. - https://api.jquery.com/clone/




    <select name="tags" class="tag_select">



  2. Now, while cloning, it will select all the elements with the same class name and result in duplicate elements. You can avoid this by retrieving only the first/last element from the cloned ones.



    <div id="tag-wrapper">
    <label for="">Tag (max 3)</label>
    <button id="add_tag">Add Tag</button>

    <div id="tag_selects">
    <select name="tags" class="tag_select">
    <option value="0">Nothing</option>
    @foreach ($tags as $tag)
    <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
    </select>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var counter = 0;
    $(document).ready(function(){
    $("#add_tag").click(function(e){
    e.preventDefault();
    counter++;
    if(counter < 3){
    $(".tag_select").clone().last().appendTo('#tag_selects');
    }
    });
    });
    </script>
    </div>



JS Fiddle working example : https://jsfiddle.net/za8h2L19/1/






share|improve this answer
























  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:27











  • You can accept the answer then :)

    – Ijas Ameenudeen
    Nov 25 '18 at 15:29














0












0








0







The real issue is your wrapper id is tag-wrapper. But you miss spelled while appending as tag_wrapper. Even though you fix this, there are 2 more issues will raise.





  1. Since you are cloning the element, you cant use id attribute any more. Give it a class name as ,




    Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. - https://api.jquery.com/clone/




    <select name="tags" class="tag_select">



  2. Now, while cloning, it will select all the elements with the same class name and result in duplicate elements. You can avoid this by retrieving only the first/last element from the cloned ones.



    <div id="tag-wrapper">
    <label for="">Tag (max 3)</label>
    <button id="add_tag">Add Tag</button>

    <div id="tag_selects">
    <select name="tags" class="tag_select">
    <option value="0">Nothing</option>
    @foreach ($tags as $tag)
    <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
    </select>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var counter = 0;
    $(document).ready(function(){
    $("#add_tag").click(function(e){
    e.preventDefault();
    counter++;
    if(counter < 3){
    $(".tag_select").clone().last().appendTo('#tag_selects');
    }
    });
    });
    </script>
    </div>



JS Fiddle working example : https://jsfiddle.net/za8h2L19/1/






share|improve this answer













The real issue is your wrapper id is tag-wrapper. But you miss spelled while appending as tag_wrapper. Even though you fix this, there are 2 more issues will raise.





  1. Since you are cloning the element, you cant use id attribute any more. Give it a class name as ,




    Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. - https://api.jquery.com/clone/




    <select name="tags" class="tag_select">



  2. Now, while cloning, it will select all the elements with the same class name and result in duplicate elements. You can avoid this by retrieving only the first/last element from the cloned ones.



    <div id="tag-wrapper">
    <label for="">Tag (max 3)</label>
    <button id="add_tag">Add Tag</button>

    <div id="tag_selects">
    <select name="tags" class="tag_select">
    <option value="0">Nothing</option>
    @foreach ($tags as $tag)
    <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
    </select>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var counter = 0;
    $(document).ready(function(){
    $("#add_tag").click(function(e){
    e.preventDefault();
    counter++;
    if(counter < 3){
    $(".tag_select").clone().last().appendTo('#tag_selects');
    }
    });
    });
    </script>
    </div>



JS Fiddle working example : https://jsfiddle.net/za8h2L19/1/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 14:43









Ijas AmeenudeenIjas Ameenudeen

6,85732847




6,85732847













  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:27











  • You can accept the answer then :)

    – Ijas Ameenudeen
    Nov 25 '18 at 15:29



















  • thank you, it fixed

    – Granite Bagas
    Nov 25 '18 at 15:27











  • You can accept the answer then :)

    – Ijas Ameenudeen
    Nov 25 '18 at 15:29

















thank you, it fixed

– Granite Bagas
Nov 25 '18 at 15:27





thank you, it fixed

– Granite Bagas
Nov 25 '18 at 15:27













You can accept the answer then :)

– Ijas Ameenudeen
Nov 25 '18 at 15:29





You can accept the answer then :)

– Ijas Ameenudeen
Nov 25 '18 at 15:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458992%2fjquery-clone-dont-do-anything%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno