<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<input type="text" id="item" onkeydown="if (event.keyCode == 13) document.getElementById('getItem').click()">
<button id="getItem" onclick="getItem()">Add to List</button>
<button onclick="getClearItem()">Clear</button> <hr color="gray" id="lineDivider">
<button onclick="getClearList()">Clear List</button> <br>
<textarea readonly id="list"></textarea>
</center>
</body>
<script>
function getItem() {
if (document.getElementById("list").value == "") {
document.getElementById("list").value += document.getElementById("item").value;
}
else {
document.getElementById("list").value += \n document.getElementById("item").value;
}
}
function getClearItem() {
document.getElementById("item").value = ""
}
function getClearList() {
document.getElementById("list").value = ""
}
</script>
</html>
This is what I am trying to do:
You enter some text into the "item" textbox and click the "Add to list" button. Then if the "List" textarea is blank it adds the text without inserting a line break. But if there is already some text in the textarea, it inserts a linebreak and then adds the text. I am not sure what I am doing wrong, or how it should even be done.
When I remove the if else statement and just use document.getElementById("list").value += document.getElementById("item").value; it works. When I add the \n to that, it does not add anything to the blank field.
I apologize if my question is unclear.