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 6145057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:44:28+00:00 2026-05-23T18:44:28+00:00

I have a simple application where if the user presses TAB or Space; a

  • 0

I have a simple application where if the user presses TAB or Space; a new input field is added to a div element.

The following is a snippet of the div element used to hold the input fields:

<div class="container" id="container" onkeydown="KeyDown(event)">
    <input type="text" maxlength="15" id="0"/>
</div>

The following is the JavaScript code I am using to add input fields:

function KeyDown(e) {
    var container = document.getElementById("container");

    if(e.which === 9 || e.which === 32) { //User pressed Tab or Space
        container.innerHTML += "<input type='text' maxlength='15' id='0'/>";
    }
}

This works, but there is a 8px margin/gap (4px more than default CSS) between the first and second text input fields. Only the first input field has an extra 4px margin, even though the CSS and HTML of the page is identical across all input fields in Chrome.

The problem is fixed by replacing the innerHTML of the container with the following Javascript:

if(c === 1) { //c is the number/count of input elements
    container.innerHTML = "<input type='text' maxlength='15' id='0'/>";
}

...
c++;

Why does this occur while adding the initial input field with HTML, versus replacing the innerHTML and them incremental each input field?

  • 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-05-23T18:44:29+00:00Added an answer on May 23, 2026 at 6:44 pm

    There will be whitespace between the two input tags (line break and spaces for your formatting). This will cause a single space to be rendered which will be your extra 4px. It should work if you change your initial html to:

    <div class="container" id="container" onkeydown="KeyDown(event)">
        <input type="text" maxlength="15" 
        id="0"/></div>
    

    Horrible formatting but a line break within a tag is not formatted.

    I think that if you append the new input instead of manipulating the innerHTML you might also, avoid the whitespace:

    function KeyDown(e) {
        var container = document.getElementById("container");
    
        if(e.which === 9 || e.which === 32) { //User pressed Tab or Space
            var newInput = document.createElement('input');
            newInput.id = "0";
            newInput.type = "text";
            newInput.maxlength = "15"
            container.appendChild(newInput);
        }
    }
    

    By the way, a numeric id is not valid – it should start with a letter What are valid values for the id attribute in html.

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

Sidebar

Related Questions

I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();
I have a simple application in which I need to let the user select
I have a simple Desktop Facebook application that allows the user to retrieve some
I have a simple windows application that pops up an input box for users
I have a simple application that shows the user an image (in a UIImageView)
I have simple application, that shows list of many items, where user can display
I have a simple tabBarController application where the user can navigate around 3 views.
I have a simple application with just a window and a user control. The
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple application using netbeans for developing and maven for building et

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.