Skip to content Skip to sidebar Skip to footer

Cloned Divs Are Not Staying In The Grid Correctly

So I have a button on the left and a form on the right. If you click the button on the left you can create up to 6 forms. If i create these forms in html manually everything displa

Solution 1:

I added id="formy" to the form and then used $("#formy").append(new_clone) to add the clones. This seems to keep your button in the upper left and keep the clones in order.

<form id="formy" onsubmit="return false" style="background:transparent; border-color:transparent;">

<div id="duplicater0" class="clone flavNameDescBox addnewflavorimg col-4" style="display:inline-block;"> 
    ADD ANOTHER FLAVOR PROFILE
    </div>

  <div id="clonedInput" class="clonedInput flavNameDescBox col-4" style="display:inline-block; clear:left;">
    <h3>Create Flavor</h3>
    <h4>Flavor profile <span class="label-nbr">1</span></h4>

    <fieldset>
      <input class="category" id="category1" placeholder="Your Web Site (optional)" type="url" tabindex="4" required>
    </fieldset>
    <fieldset>
      <textarea placeholder="Type your message here...." tabindex="5" required></textarea>
    </fieldset>
    <fieldset>
      <textarea class="textarea2" placeholder="Type your message here...." tabindex="5" required></textarea>
    </fieldset>

    <button class="remove">Remove</button>
  </div>
</form>

<script>

var cloneIndex = 1;
var clones_limit = 5;
var cloned_nbr = $(".clonedInput").length-1; //Exclude Default (first) div 

function clone()
{
  if(cloned_nbr<clones_limit)
  {
    cloneIndex++;
    cloned_nbr++;

    var new_clone =  $(".clonedInput").first().clone();

    new_clone.attr("id", "clonedInput" +  cloneIndex);
    new_clone.find(".label-nbr").text(cloneIndex);
    new_clone.find(".category").attr("id","category"+cloneIndex);
    new_clone.show(".remove").attr("id","remove"+cloneIndex);
    new_clone.on('click', 'button.clone', clone);
    new_clone.on('click', 'button.remove', remove);

    $("#formy").append(new_clone);
  }
}
function remove(){
  if(cloneIndex>1){
    $(this).parents(".clonedInput").remove();
    cloned_nbr--;
  }
}
$(".clone").on("click", clone);

$(".remove").on("click", remove);

</script>

Post a Comment for "Cloned Divs Are Not Staying In The Grid Correctly"