When I am defining my input tag in html and accessing in JS by id then I am getting my tag.
HTML Code:
JavaScript
x
2
1
<input class="easyui-combobox" name="language" style="width:30%;" id= "XX">
2
JS Code:
JavaScript
1
2
1
var cc = document.getElementById("XX");
2
Here things are fine.
But When I am creating from javascript and trying to access I am getting. I want dynamic So I need to create from JS.
JS Code:
JavaScript
1
5
1
var input = document.createElement("input");
2
input.className = 'easyui-combobox';
3
input.style = 'width:30%';
4
input.id = "XX";
5
Here I am getting null after applying this:
JavaScript
1
2
1
var cc = document.getElementById("XX");
2
Advertisement
Answer
You have to append your created element into the document using document.body.appendChild(input);
JavaScript
1
6
1
var input = document.createElement("input");
2
input.className = 'easyui-combobox';
3
input.style = 'width:30%';
4
input.id = "XX";
5
document.body.appendChild(input);
6
console.log(document.getElementById("XX"));