The following code creates a text input field AFTER the Hey girls! paragraph when pressing on the push button:
<html>
<head>
<script>
function func()
{
var elem=document.createElement("input");
document.body.appendChild(elem);
elem.setAttribute('type','text');
}
</script>
</head>
<body>
<p>Hey guys!<p>
<input type="button" value="push" onclick="func()"/>
<script>
func();
</script>
<p>Hey girls!<p>
</body>
</html>
How do I make sure the text input fields are created before the Hey girls! paragraph and after the Hey guys! paragraph?
Thats a job for
HTMLElement.prototype.insertBefore.Synopsis: parentElement.insertBefore(newElement, referenceElement);
Example:
Since you haven’t any ID or classname for your paragraph node here, I just referenced that with
.getElementsByTagName. Would make things easier if you have a more accurate query possibility.Ref.: https://developer.mozilla.org/en-US/docs/DOM/Node.insertBefore