I'm starting with javascript, from scratch, I have this exercise where the goal is to add and remove city names in a list in html. I know little of DOM. The insert part already works, but the removal part is not working. Can anyone help me?
<h1>Lista de cidades visitadas.</h1>
<ul id="city"></ul>
<p>Adicionar ou remover uma cidade da lista de cidades a serem visitadas.</p>
<input type="text" name="txt-city" id="txt-city">
<button onclick="addCity()">Adicionar Cidade</button>
<button onclick="removeCity()">Remover cidade</button>
<script>
function addCity() {
var nameCity = document.getElementById("txt-city").value;
var newList = document.createElement("li");
var contentText = document.createTextNode(nameCity);
newList.appendChild(contentText);
document.getElementById("city").appendChild(newList);
}
function removeCity() {
var list = document.getElementById("city");
var el = list.getElementsByTagName("li");
var txt = document.getElementById("txt-city").value;
var content = document.createTextNode(txt);
var x;
for(x = 0; x < el.length; x++) {
if(content == el[x].value) {
list.removeChild(list.childNodes[x]);
}
}
}
</script>