Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo pixels added to absolute position of elements added via javascript in chrome only
    primarykey
    data
    text
    <p>Here's my first question on stackoverflow. I've used you guys to answer many many questions in the past, but this time, I wan't able to find what I was looking for, so any help would be appreciated!</p> <p>I have a simple website I was playing with to brush up on css, javascript, jQuery, and html. I have an issue where I want to add an input textbox via javascript directly below the lowest existing textbox. It seems fairly simple, and it works properly in ie9 and firefox, but in chrome, each new textbox is pushed over to the right by 2 pixels.</p> <p>Here is the html and javascript code:</p> <p>html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;header&gt; &lt;h3&gt;Header to the 3rd degree!&lt;/h3&gt;&lt;/header&gt; &lt;section class="textboxes"&gt; &lt;input type="textbox" class="duplicate" /&gt; &lt;/section&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>javascript</p> <pre><code>$(function(){$('.duplicate').attr('onclick', 'createInputBox()'); }); function createInputBox(){ var inputs = $('.duplicate'); var lastInput = inputs[inputs.length - 1]; var position = getAbsolutePosition(lastInput); var x = position[0]; var y = position[1] + 25; $('.textboxes').append(inputTextBoxOpenTag + "style=\"position:absolute; left:" + x + "px; top:" + y + "px;\"&gt;" + inputTextBoxCloseTag ); } function getAbsolutePosition(object){ var curleft = curtop = 0; if(object.offsetParent){ do{ curleft += object.offsetLeft; curtop += object.offsetTop; } while(object = object.offsetParent); return [curleft, curtop]; } } var inputTextBoxOpenTag = "&lt;input type=\"textbox\" class=\"duplicate\" onclick=\"createInputBox()\" " var inputTextBoxCloseTag="&lt;/input&gt;" </code></pre> <p>So the basic idea is every time you click in a text box, a new one is added below the last textbox.</p> <p>The new textbox position is set using the current position of the last textbox. After debugging in chrome, I could see that the horizontal position, x, was being set to the actual value I wanted. However, when the function was called again, it claimed that the actual position was 2 pixels greater than what it had set it to in the last function call.</p> <p>Though this page obviously isn't meant to be used for anything important, I feel knowing what is causing this issue may give me more information on why chrome is displaying some textboxes on another site I'm working on out of line, while firefox and ie9 display them just fine.</p> <p>Thanks again for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload