How to copy an html block when clicking a link/button?

Asked

Viewed 1,160 times

5

Today a friend and I thought about developing a system for registering resumes, only I need to click on a link add 1 more block inputs each time clicked. I tried searching, and did not find nice fonts, ie did not know how to search.

See the image below to understand better:

inserir a descrição da imagem aqui

Note: I am using Codeigniter if he has any helper do that.

  • Show your code. What you’ve tried?

  • I haven’t tried anything yet, I’m early looking for something on the subject and did not find, so I decided to post here to the people try to help me. Like I said, I’m young, and I don’t have much logic.

  • 2

    You must have done something. Otherwise, you should start from the beginning. Also, why the Jquery tag? Why you assume that the solution to the problem is in Jquery?

  • Because I know that jQuery can use ajax not to reload the page. I’ve seen an example almost similar to what I want to do, but just brush and show.

  • 1

    Then you need to do AJAX? You should mention this in the question. And, yes, Jquery has facilities for AJAX, but it’s not required. Jquery is just a library. Why don’t you start by trying to learn how to do AJAX and, after you’re already getting the data (printing on the screen, for example), go back and ask a more specific question? It is difficult to answer such wide questions even if we try. It would not be an answer but a tutorial

  • Okay so guys, thanks for the info, I thought it was simple stuff and you would understand what I want, but all right, I’ll look for sources anything I create an answer later, or else I can develop a more specific question. Excuse me

  • 4

    @Guillermo does not need to apologize, only that he does not want to solve everything in one question. Go assembling your code, or seeing examples here on the site, and when you have a question about each of the steps, you can ask separately how to solve each one. The important thing is to make it happen step by step. We’re all here to help, but the questions better be very objective. Here are some cool tips: [Ask] and Community FAQ has some things that give a better idea of how the site works.

  • All right then, thank you @Bacco and Plabo

  • 1

    @See if this helps you: How to make a dynamic form?.

  • @Bacco, this will help me in something else, incredible, but that’s not what I wanted, but I ended up finding it here in Stack Over Flow, that’s what I intend to do: http://jsfiddle.net/MDL48/

  • 2

    @Great Guillermospinxo you found. Take a peek at the links I’ve recommended, on how to ask, so when you have questions and ask new questions, you can take better advantage of the site. The community will be easier to help if the question is more objective (and contains some code to be used as a basis). So you "help us help you" :)

  • Oops, all right @Bacco I’ll read it now.

Show 7 more comments

2 answers

3


Look at that:

  • Insert several <input> unadjusted name, they won’t even be submitted.
  • Insert several <input> with the same value in the attribute name, only the last will be submitted.

What you are wanting to do deserves a little more care, it is not simply insert a <input> behind each other. In fact, it may be, since you do not need to recover this data later and I believe that the goal is not this.

What you can do is:

  • Have several <input> with the attribute name being an array. On the server you by iterate over that array and retrieve the data. For example:
<input type='text' name='empresas[]' placeholder='Nome da empresa 1'/>
<input type='text' name='empresas[]' placeholder='Nome da empresa 2'/> <!-- s/ problemas -->

Or else...

  • Set the value of the attribute name being empresa-x, where x is the "field index" and this number should be incremented each time a new input is entered in the form. In this case you must clone the last element, increment the value of x and insert at the end of the form.
<input type='text' name='empresa-1' placeholder='Nome da empresa 1'/>
<input type='text' name='empresa-2' placeholder='Nome da empresa 2'/>
<!-- ... -->

The first is simpler, because all you have to do is clone the element. The second is a bit more boring to do because it is necessary to increase the index number and replace all attributes name, but nothing complicated. It will depend on how you want to get this data on the server later.

Cloning with Array

$(function(){
  $('button').on('click', function(){
    $('.info-block:first').clone().insertAfter('.info-block:last');
  });
});
.info-block { padding: 10px; border: 1px solid #ccc }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='container'>
 
  <!-- bloco que será clonado -->
  <div class='info-block'>
    <input type='text' name='empresas[]' placeholder='Empresa'/>
    <input type='text' name='escolas[]' placeholder='Escola'/>
    <!-- outros campos... -->
  </div>
</div>

<button>Adicionar + campos</button>

Cloning

In this second example, to make it easier to control the index I created a <span> as if it were an accountant. Through it it is possible to obtain the last index to increment it and clone the block.

$(function(){

  $('button').on('click', function(){
    
    var $bloco = $('.info-block:last').clone(), 
        indice  = parseInt($bloco.find('span').text()) + 1; 
    
    $bloco.find('span').text(indice);
    $bloco.find('input[name^="empresa-"]').attr('name', 'empresa-' + indice);
    $bloco.find('input[name^="escola-"]').attr('name', 'escola-' + indice);
    
    // Se ouver outros inputs, altera o atributo name deles com o índice
    
    $bloco.insertAfter('.info-block:last');
  
  });
});
.info-block { padding: 10px; border: 1px solid #ccc }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='container'>

  <!-- bloco que será clonado -->
  <div class='info-block'>
    <span>1</span>
    <input type='text' name='empresa-1' placeholder='Empresa' />
    <input type='text' name='escola-1' placeholder='Escola' />
    <!-- outros campos... -->
  </div>
</div>

<button>Adicionar + campos</button>

  • 1

    It worked perfectly, better than what I had done before thanks @Renan

  • Renan, I’m involved in a similar project, so I need to do it again... has how to generate the name different? example name="experiencia1" name="experiencia2" name="experience3", sorry to reactivate the topic, but I preferred to reuse rather than create another.

  • 1

    The second example does not solve? Cloning incrementing.

  • Gave right worth.

  • @Renangomes as it would be to remove a cloned block?

2

Assuming the html file:

<div class='dados'>
  <input type="text" value="" />
</div>
<a href="javascript:void(0);" id="btnAdd">+</a>

By clicking the button, through a jQuery event, you solve the problem:

$("#btnAdd").click(function() {
    $(".dados").append('<input type="text" value="" />');
});

Test with this fiddle

  • I’ll test it, if it works I mark as solved, but a doubt, in place in the append('posso trocar aqui pra o id da div que quero que duplique?')

  • if you want to duplicate a div with a certain id, you can: $(".dados").append($("#id_da_div_para_duplicar"));

  • It worked, but I changed the append(), put append("#formCursos") and instead of printing the form on the screen, appeared the string #formCursos

  • is missing the "$": .append($("#formCursos"));

Browser other questions tagged

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