Method . jQuery clone()

Asked

Viewed 576 times

1

I need it every time I click the div Mais, clone the full content of it and put it right below. However it’s not working, could you tell me what I’m missing ?

$(document).ready(function () {
  $('#maisfoto').on('click', function () {
    $( ".fileinput fileinput-new" ).clone().appendTo( ".fileinput fileinput-new" );
  });
});
    
div#maisfoto {
  text-align: center;
  color: white;
  float: right;
  width: 43px;
  height: 21px;
  border-radius: 20px;
  background-color: brown;
  vertical-align: middle;
  display: table;
  padding-bottom: 3px;
  margin-bottom: 10px;
  cursor: pointer;
}
<div id="maisfoto">Mais</div>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div id="tamb" class="fileinput-preview fileinput-exists thumbnail" style="padding-bottom: 15px;">Tamanho esperado: 586px x 184px</div>
    <div>
        <span class="btn btn-default btn-file"><span class="fileinput-new">Selecionar Imagens</span><span class="fileinput-exists">Trocar</span><input type="file" name="..."></span>
        <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remover</a>
    </div>
</div>

1 answer

2


Edit 2:Upon request of image preview

everything working with plugin, boot, add, remove and preview follows code, ta giving some errors on the console, ai you give a look and correct ;)

	<html>
		<style>
			div#maisfoto {
			text-align: center;
			color: white;
			float: right;
			width: 43px;
			height: 21px;
			border-radius: 20px;
			background-color: brown;
			vertical-align: middle;
			display: table;
			padding-bottom: 3px;
			margin-bottom: 10px;
			cursor: pointer;
		  }
		</style>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">    
		<!-- Latest compiled and minified JavaScript -->
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
		
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.css"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.css.map"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.min.css"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/js/jasny-bootstrap.min.js"></script>
		<body>
			<div id="maisfoto">Mais</div>  
			
			<div class="fileinput fileinput-new aquiClonar" data-provides="fileinput">
				<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
				<div>
					<span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="..."></span>
					<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
				</div>
			</div>
		</body>
		<script>
			$(document).ready(function () {
				$('#maisfoto').on('click', function () {
					$( ".aquiClonar:first" ).clone().appendTo( "body" );              
					
					$('.aquiClonar a').click(function(){
						console.log('remover()');
						 $(this).parents('.aquiClonar').remove();
					});
				});
				
			  });                  
		</script>
	</html>

Code comments

    $(document).ready(function () { // quando terminar de carregar a pagina
            $('#maisfoto').on('click', function () { // ao clicar na div id maisfoto
                $( ".aquiClonar:first" ).clone().appendTo( "body" ); // procura a class aquiClonar e pega o primeiro (first) e add na tag body

                $('.aquiClonar a').click(function(){ // ao clicar em uma tag a que contenha class aquiClonar faz o evento click
                    console.log('remover()'); // log para debug
                     $(this).parents('.aquiClonar').remove(); // this se referencia ao selector a que por sua vez chama a classe superior aquiClonar e seleciona, após isso ele remove a tag e tudo que conter nela
                });
            });

          });    

Edit 3: Request not to remove the first element:

    $(document).ready(function () { // quando terminar de carregar a pagina
            $('#maisfoto').on('click', function () { // ao clicar na div id maisfoto
                var clone = $('.aquiClonar:first').clone(); // clonar
                clone.append("<a href='#' class='btn btn-default fileinput-exists' data-dismiss='fileinput'>Remove</a>"); // Adicionar o remove somente nos clones
                $( "body" ).append(clone); // procura a class aquiClonar e pega o primeiro (first) e add na tag body

                $('.aquiClonar a').click(function(){ // ao clicar em uma tag a que contenha class aquiClonar faz o evento click
                    console.log('remover()'); // log para debug
                     $(this).parents('.aquiClonar').remove(); // this se referencia ao selector a que por sua vez chama a classe superior aquiClonar e seleciona, após isso ele remove a tag e tudo que conter nela
                });
            });

          }); 
  • It worked out more precisely that when you open the preview come only the preview of the child I want, can you help me do this ? I am using http://www.jasny.net/bootstrap/javascript/#fileinput to mount the preview

  • Another thing when clon for the second time, it create double, because of the class, then every class with the same name it clones.

  • 2

    @Segio I will comment on the sources for a moment...

  • has to block to not be able to delete the last, because if the person is to remove and remove all and if the last is together from this, the "more" no longer works.

  • @Renanrodrigues made one more e-dit ai in the answer to solve the problem of the first element not remove

  • vlw man. helped a lot

  • I forgot to tell you that the first element will be without the tag remove... but already got it by the way right

  • Yeah. It worked just fine

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.