Reputation: 153
I want to write some text, click a button and the text added has to make a list.
This is what I have at the moment :
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>3-2</title>
<script type="text/javascript" src="js/oefening3-2.js"> </script>
</head>
<body>
<p> <span> geef een tekst in<input type="text" id="waarde"> <input type="button" value="Voeg toe" onClick="a();"></span></p>
<ol>
<p> <span id="waarden"> waarde </span> </p>
</ol>
And the Javascript code :
function a()
{
var ingevoerd = document.getElementById("waarde").innerHTML;
document.getElementById("waarden").add(ingevoerd);
}
I can understand it doesnt make much sence but I have not really a clue how to do this. Enter some text, click the button and the text has to be in a not numbered list. Can annyone help me out? Would be much appreciated!
Kind regards!
Upvotes: 1
Views: 3332
Reputation: 12079
HTML:
geef een tekst in<input type="text" id="waarde">
<input type="button" value="Voeg toe" onClick="a()">
<ul id="waarden"></ul>
Javascript code:
function a() {
var node = document.createElement("LI");
var textnode = document.createTextNode(document.getElementById("waarde").value);
node.appendChild(textnode);
document.getElementById("waarden").appendChild(node);
}
JSFiddle demo: http://jsfiddle.net/BloodyKnuckles/y1fk1q70/
Explanation:
The HTML consists of a few elements:
The button click handler is instructed to execute function a
when clicked.
The Javascript then performs the following steps:
Upvotes: 4