This problem seemed so general that I wanted to refrain from asking it as a question here, however I seemed to have a real hard time finding any information regarding given problem. When I am trying to add basic HTML buttons, both <input type="button">
and <button>
by a JavaScript click function (or no click at all), given added buttons will appear different. Examples can be found below:
I am trying to achieve the following with even space between buttons:
$(document).ready(function() {
$("<button>Inserted</button>").insertAfter("button");
//$("button").after("<button>Inserted</button>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>
Button 1
</button>
<button>
Button 2
</button>
<button>
Button 3
</button>
Basic example with <button>
elements:
$(document).ready(function() {
$("button").click(function() {
var btn = document.createElement("button");
var t = document.createTextNode("Added");
btn.appendChild(t);
document.body.appendChild(btn);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>
Button 1
</button>
<button>
Button 2
</button>
<button>
Button 3
</button>
Basic example with <input>
elements:
$(document).ready(function() {
$("input").click(function() {
var btn = document.createElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Added");
document.body.appendChild(btn);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" value="Button 1" />
<input type="button" value="Button 2" />
<input type="button" value="Button 3" />
What is causing the button elements to behave this way or are given elements missing something, like any properties perhaps? By default, an element added by HTML that should appear as a button, would have certain spacing, but this would not be the case when adding this by JavaScript. Also should be noted that this spacing differs by browser. I want "JavaScript added buttons" to behave just like "HTML added buttons".
Edit: According to the answers the problem is clearly whitespace.
Now my question still remains: How could I make newly added buttons by JavaScript behave the same way as newly added buttons by HTML? Is there a way to detect how much whitespace is in between and add this to the buttons so that they will always behave the same way?
I know whitespace is usually unwanted and should be avoided, but I also want to refrain from using weird solutions to make my HTML work when my HTML is working just fine by default when not running any JavaScript.