I'm aware this has been asked before, but my code isn't working.
My assessment task is to populate something similar to a list box from a JavaScript array and then delete the value. I thought my code should work, but it won't.
Markup:
var sel = document.getElementById('cars');
var carArray = ["Audi", "BMW", "Porsche"]
for (var i = 0; i < carArray.length; i++) {
var listBox = document.createElement('option');
listBox.innerHTML = carArray[i];
listBox.value = carArray[i];
sel.appendChild(listBox);
}
function deleteFunc() {
var selInd = document.getElementById("cars").selectedIndex;
carArray.splice(selInd - 1, selInd + 1);
}
<form>
<select id="cars" multiple>
<option id="carBrand"></option>
</select>
<button onclick="deleteFunc()">Delete</button>
</form>
selectinput ?type="button", otherwise it will act as a submit button which will post the form and thus reload the page