Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!