0

I don't know where to put the counter; The next cloned id must be gender_1

Example:

<select id="gender">

Next is:
`<select id="gender_1">`

then:
`<select id="gender_2">`

https://jsfiddle.net/f7hjh1gf/6/

2
  • jsfiddle.net/f7hjh1gf/6 Commented Jul 17, 2017 at 10:23
  • 4
    Please go read How to Ask. Relevant code belongs directly into your question, and not just on external sites only. Commented Jul 17, 2017 at 10:24

2 Answers 2

2

Try this, before append to the element you need to replace the cloned selected element id using find() function and update the id incremented using c++

Updated jsfiddle

$(document).ready(function(){
var c=1;
    $(document).on('click', '#mode', function(){
        $("#notok").clone(true).find('select').attr('id','gender_'+(c++) ).closest('.ok').appendTo("#clones");
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I think op was missing with jquery tag..he was full coded with jquery function.
@J.A.Manato, you have also not marked the answer in your previous post, you should mark answer or upvote if any of the post or reply helps you.
1

Everytime you add a clone get the number of clones already present and increment it by one before adding it to "gender_", you can use $("#clones div").length to get number of clones currently present.

$(document).ready(function(){
    $(document).on('click', '#mode', function(){
        $("#notok").clone(true).attr('id', "gender_"+($("#clones div").length + 1)).appendTo("#clones");
    });
});
<div class="container">

<div class="ok" id="notok">
	<select id="gender">
    	<option value="Male">Male</option>
        <option value="Female">Female</option>
    </select>

    
</div>

<div id=clones></div>

<button id="mode">Clone</button>


  

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Modal Header</h4>
    </div>
    <div class="modal-body">
      <p>Some text in the modal.</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
  
</div>
  </div>
  
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.