Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
How to add new lines after HTML elements

I have a simple method for adding input boxes after a button is clicked. The goal of this method is to generate a set of input boxes with a newline inserted after each div.

enter image description here

In the screenshot above you can see that the divs are spaced properly. However, when the add_more button is clicked the generated inputs do not come out properly.

Expected: The code should generate new input boxes like so:

                      <div>
                        Key Term 2: <input id="el2" type="text" value=""> <br>
                      </div>
                      <br>

Actual:

function add_more() {
  // we've added more inputs.
  addMore = true;

  // set html generated to false, because new inputs have been added.
  htmlGenerated = false;

  // increment the number of inputs.
  numberOfInputs++;

  //fetch the input boxes.
  inputs = document.getElementById("inputBoxes");

  // create newline
  br_key = document.createElement("br");

  // create newline
  br_description = document.createElement("br");

  //create a new row for a key term.
  row = document.createElement("div");

  // set the key term text.
  row.innerHTML = "Key Term ";
  row.innerHTML += numberOfInputs;
  row.innerHTML += " :";

  // create the input for the key.
  key = document.createElement("input");
  key.setAttribute("id", "el" + numberOfInputs);

  //add the key to the row.
  row.appendChild(key);
  row.after(br_key);

  //create a row for the new description.
  row2 = document.createElement("div");

  // set the description text.
  row2.innerHTML = "Description  "
  row2.innerHTML += numberOfInputs;
  row2.innerHTML += " :";

  // create the description input
  description = document.createElement("input");
  description.setAttribute("id", "dl" + numberOfInputs);

  // add the description to the row.
  row2.appendChild(description);
  row2.after(br_description);

  // add the rows for the key and the description to the inputBoxes.
  inputs.appendChild(row);
  inputs.appendChild(row2);
}
<div>Key Term 5 :<input id="el5"></div>

Any help figuring out this issue would be greatly appreciated. Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your issue here is essentially incorrect HTML, CSS. I'd implement your inputs, etc. like this:

.full-width-label {
  display:block;
}
<label class="full-width-label" for="1">Label</label>
<input type="text" id="1"/>

There are multiple ways to achieve the above, this is just one of your options but now you no longer need to embed the look into the HTML and the format of your HTML (line breaks) is independent of your look.

You might want to look into an off the shelf solution for these kinds of things, like Bootstrap or Tailwind

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use make it in a simple way

HTML

add this jquery cdn

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> 
</script>

<button type="button">Click Here</button>
<div class="appendDiv">

</div>

Js

 $(document).ready(function (){
    var button = document.getElementsByTagName('button');
    var appendDiv = document.getElementsByClassName('appendDiv');
    var key = 1;
    var descKey = 1;

    $('button').click(function(event){
    event.preventDefault(); 

      $(appendDiv).append('<div class="child"><div class="grand-child"><label>Key Form :'+ ' '+(++key)+'</label><input type="text" value=""/></div><div class="grand-child"></div><label>Description :'+ ' '+(++descKey )+'</label><input type="text" value=""/></div>');
   })
}) 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda