-1

I have jQuery click event which is create group of div or container for every clicking the button . All I want is to create a container every click on a button.

Here is the HTML container create on every button click.

<button type="button" id="add_container">Add Container</button

<div class="container">
 <div class="label">
  <span>I'm the Label</span>
 </div>
 <div class="margin-select">
  <div class="padding-select">
   <select class="select2>
    <option>Choose 1</option>
    <option>Choose 2</option>
   </select>
  </div>
 </div>
</div>

jQuery:

$('#add_container').on('click', function(){
  //add a container every click event <div class="container">...</div>
})
2
  • Does this answer your question? How to append HTML using jQuery Commented Jun 20, 2022 at 8:23
  • If the .container element always exists in the DOM when the page loads then you can use clone() and append() Commented Jun 20, 2022 at 8:32

3 Answers 3

3

First Create a dummy div as shown below -

HTML -

<button type="button" id="add_container">Add Container</button

<div class="repeat_container"></div>

JQuery -

$('#add_container').on('click', function(){
   var html = '<div class="container"><div class="label"><span>I'm the Label</span></div><div class="margin-select"><div class="padding-select"><select class="select2><option>Choose 1</option><option>Choose 2</option></select></div></div></div>'; 
  $('.repeat_container').append(html);
});

Hope it helps !!

Sign up to request clarification or add additional context in comments.

Comments

0

Use clone() with .insertAfter.

Example:

$('#add_container').on('click', function() {
  $(".container:first").clone().insertAfter("div.container:last")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="add_container">Add Container</button

<div class="wrapper">
  <div class="container">
    <div class="label">
      <span>I'm the Label</span>
    </div>
    <div class="margin-select">
      <div class="padding-select">
    <select class="select2">
    <option>Choose 1</option>
    <option>Choose 2</option>
   </select>
  </div>
 </div>
</div>
</div>

Comments

0

You can add a container wrapper and append a child to it:

HTML:

<button type="button" id="add_container">Add Container</button>

<div class="wrapper">
    <div class="container">
        <div class="label">
            <span>I'm the Label</span>
        </div>
        <div class="margin-select">
            <div class="padding-select">
                <select class="select2">
                    <option>Choose 1</option>
                    <option>Choose 2</option>
                </select>
            </div>
        </div>
    </div>
</div>

jQuery:

$('#add_container').on('click', function(){

    console.log('Clicked!', 'add_container');

    $('.wrapper').append(
        '<div class="container">'
        + '<div class="label">'
            + '<span>I\'m the Label</span>'
        + '</div>'
        + '<div class="margin-select">'
            + '<div class="padding-select">'
                + '<select class="select2">'
                    + '<option>Choose 1</option>'
                    + '<option>Choose 2</option>'
                + '</select>'
            + '</div>'
        + '</div>'
    + '</div>');
})

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.