Reputation: 11
I'm new to this website and I'm just learning how to code. My program doesn't write anything or create an input, can someone help me and spot the mistake?
<html>
<head><title>array exchange</title></head>
<body>
<form id="ne" >
number of array elements: <input type="number" name="el_array" ><br>
<input type="submit" value="execute" onsubmit="create()">
</form>
</body>
<script>
function create(){
var numel = document.getElementById("ne");
var x = document.createElement("INPUT");
x.setAttribute("type", "number");
}
</script>
</html>
Upvotes: 1
Views: 72
Reputation: 705
var input = document.createElement("input");
input.type = "submit";
document.body.appendChild(input);
input.addEventListener("click",function createEl(){
var numel = document.getElementById("ne");
var x = document.createElement("INPUT");
x.setAttribute("type", "number");
numel.appendChild(x);
});
<form id="ne" >
number of array elements: <input type="number" name="el_array" >
<br>
</form>
Upvotes: 1
Reputation: 53
İ recommend starting read from MDN.My opinion MDN The best documentation for Javascript:
https://developer.mozilla.org/bm/docs/Web/JavaScript/Guide/Introduction
Upvotes: 1
Reputation: 77
You can read about the usage of the form tag as well for better understanding:
Upvotes: 1
Reputation: 133453
You need to append child then it will be reflected in DOM
numel.appendChild(x)
Also use type="button"
instead of type="submit"
and onclick
event handler
<input type="button" value="execute" onclick="create()">
function create() {
var numel = document.getElementById("ne");
var x = document.createElement("INPUT");
x.setAttribute("type", "number");
numel.appendChild(x);
}
<form id="ne">
number of array elements: <input type="number" name="el_array"><br>
<input type="button" value="execute" onclick="create()">
</form>
Upvotes: 2