Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:29:07+00:00 2026-06-08T19:29:07+00:00

Possible Duplicate: Ignore whitespace in HTML I am creating a Dutch recipe website. The

  • 0

Possible Duplicate:
Ignore whitespace in HTML

I am creating a Dutch recipe website. The problem is a visual error that occurs when adding new form fields through Javascript.

Where the problem occurs

In order to describe all the needed ingredients, I allow the user to add more ingredient form fields to the existing ones, which are defined beforehand by HTML. You can add these new fields by pressing the button that says ‘Add another ingredient’. A javascript function called addIngredient() is called onclick (it is truncated for clarity, full code can be found here):

function addIngredient() {
  ingredientCount++;

  //define the elements that should be created
  var Group = document.createElement("span");
  var Quantity = document.createElement("input");
  var Unit = document.createElement("input");
  var Type = document.createElement("input");
  var lineBreak = document.createElement("br");

  //set the attributes for each element
  Group.setAttribute("id", "ingredient_" + ingredientCount);
  Quantity.setAttribute("type", "text");
  Unit.setAttribute("type", "text");
  Type.setAttribute("type", "text");

  //add the ingredient group span and linebreak to the ingredients div
  var ingredientList = document.getElementById("ingredienten");
  ingredientList.appendChild(Group);
  ingredientList.appendChild(lineBreak);

  //add the ingredient form elements to the ingredient group span
  var addElements = document.getElementById("ingredient_" + ingredientCount);
  addElements.appendChild(Quantity);
  addElements.appendChild(Unit);
  addElements.appendChild(Type);
}

What happens

The input elements added through JavaScript appear to have different spacing between them from those created beforehand, by HTML. Here is a screencap showing the visual error:

spacing discrepancy

This surplus spacing disappears when I remove my returns from the HTML defining the input elements that are created beforehand.

with returns:

<span>
  <input>
  <input>
  <input>
</span><br>

without returns:

<span>
  <input><input><input>
</span><br>

This leads me to conclude that the elements created with HTML actually are influenced in spacing or position by the way I write my code. The form elements created with Javascript are, of course, generated without any linebreaks, so these surplus spaces are not present.

My question

Frankly, I’m a bit shocked that hitting return in my HTML apparently outputs some spacing on the webpage. I find it ridiculous that code formatting would result in things to be added on the page. I do not want to remove the returns in my code to prevent it from happening.

Is there any other way than removing returns from my code to ensure that those spaces between my form elements do not show up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:29:09+00:00Added an answer on June 8, 2026 at 7:29 pm

    Within an HTML element, whitespace is normalized to a single space. If you have some text, seven space characters, and some more text, those seven space characters will be normalized to a single space. Similarly, if you have some text, a return character, and some more text, that return character will be normalized to a single space.

    You may think this is undesirable, but there would be at least as many odd situations if returns were just silently removed.

    You can’t prevent those returns resulting in there being a space between your input elements, but you could dynamically insert a space after each dynamically-added input element. That way there’s a space between all of the inputs, the ‘hardcoded’ ones and the ones you’ve added via script.

    Without any attempt to make the code look pretty:

    var addElements = document.getElementById("ingredient_" + ingredientCount);
    addElements.appendChild(Quantity);
    addElements.appendChild(document.createTextNode(" "));
    addElements.appendChild(Unit);
    addElements.appendChild(document.createTextNode(" "));
    addElements.appendChild(Type);
    

    That (or something along those lines) should space the dynamic inputs in the same way that the hardcoded ones are spaced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Replace words in a string, but ignore HTML Is it possible to
Possible Duplicate: how to ignore files in kiln/mercurial using tortoise hg “that are part
Possible Duplicate: Serialization version uid in Java We know that Java serialization mechanism ignore
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: SQL server ignore case in a where expression basically I need to
Possible Duplicate: Mercurial: How to ignore changes to a tracked file I work on
Possible Duplicate: Does Firefox support position: relative on table elements? Does Firefox ignore relative
Possible Duplicate: Ignore SVN files when exporting a WAR file from Eclipse? Using eclipse
Possible Duplicate: Make .gitignore ignore everything except a few files Is it possible to
Possible Duplicate: Regular expression, split string by capital letter but ignore TLA I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.