R
RH
I'm a newbie, and just starting to use javascript. I've written a small program that simply adds an element to an unordered list in the DOM:
<body>
<form>
<input type="text" name="filePath" size="100" />
<button id="add_button">Add Element</button>
</form>
<ul id="test"></ul>
<script language="JavaScript" type="text/javascript" src="viewer.js">
</script>
</body>
script in viewer.js:
window.onload = init();
function init() {
var button = document.getElementById("add_button");
button.onclick = create;
}
function create() {
// create the element in the DOM
var listItemViewer = document.getElementById("test");
var test = document.createElement("li");
test.innerHTML = "test list item";
listItemViewer.appendChild(test);
}
If I run it and click the add button, it flashes the "test list item" on the screen - if I single step it through the debugger, it shows that it puts the element in the DOM at the end of the create() function, but then it gets stripped out? Can anyone help me understand why this is happening?
Thanks,
<body>
<form>
<input type="text" name="filePath" size="100" />
<button id="add_button">Add Element</button>
</form>
<ul id="test"></ul>
<script language="JavaScript" type="text/javascript" src="viewer.js">
</script>
</body>
script in viewer.js:
window.onload = init();
function init() {
var button = document.getElementById("add_button");
button.onclick = create;
}
function create() {
// create the element in the DOM
var listItemViewer = document.getElementById("test");
var test = document.createElement("li");
test.innerHTML = "test list item";
listItemViewer.appendChild(test);
}
If I run it and click the add button, it flashes the "test list item" on the screen - if I single step it through the debugger, it shows that it puts the element in the DOM at the end of the create() function, but then it gets stripped out? Can anyone help me understand why this is happening?
Thanks,