I'm creating a filter where after you've selected what you want to filter I create span tag with your selection. My end goal is that when you click on the span tag it will be removed from the list. Problem with a simple test function that changes the text of the span on click, the function is being called as soon as the span is created and instead of when it's clicked on
<!DOCTYPE html>
<html>
<body>
<form>
<select id="Catergories">
<option value="blank"></option>
<option value="country">Country</option>
<option value="city">City</option>
<option value="LastName">Last Name</option>
<option value="company">Company</option>
</select>
<label>Enter filter here: </label><br>
<input type="text" id="namehere"><br>
<div class="col-md-6">
<button type="button" onclick="displayname()">Add Filter</button>
</div>
<div id="demo"></div>
</form>
</body>
</html>
<script>
var filterCategory = [];
var filterValue = [];
function displayname() {
filterCategory.push(document.getElementById("Catergories").value);
filterValue.push(document.getElementById("namehere").value);
var span = document.createElement('span')
span.id = document.getElementById("Catergories").value + "_" + document.getElementById("namehere").value;
span.style.width = 500;
span.style.height = 500;
span.style.backgroundColor = "red";
span.innerHTML = "Category: " + document.getElementById("Catergories").value + " Filter value: " + document.getElementById("namehere").value;
document.getElementById("demo").appendChild(span);
span.onclick = "deleteSpan(span)";
}
function deleteSpan(element)
{
element.innerHTML = "test";
}
</script>