With this code I can select the link, show its value in INPUT TEXT
it and not repeat them in the array obtained at the end.
My question is the following:
If I press the link the first time, it SHOWS ITS TEXT IN THE INPUT TEXT
.
How can I clear the link text if the link is selected a second time?
var arrDatos = new Array();
function add(x) {
var txtInput = document.getElementById('node');
var strValor = x.innerHTML;
arrDatos.pushIfNotExist(strValor, function(e) {
return e === strValor;
});
txtInput.value = arrDatos.toString();
}
Array.prototype.inArray = function(comparer) {
for (var i = 0; i < this.length; i++) {
if (comparer(this[i])) return true;
}
return false;
};
Array.prototype.pushIfNotExist = function(element, comparer) {
if (!this.inArray(comparer)) {
this.push(element);
}
};
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input class="tf w-input" id="node" maxlength="256" placeholder="Vida nocturna, el aeropuerto " type="text" size="200">
<a class="btn desc fav w-button" id="parque1" name="parque" onclick="add(this);">PARQUE</a>
<a class="btn desc fav w-button" id="parque2" name="parque" onclick="add(this);">CENTRO COMERCIAL</a>
<a class="btn desc fav w-button" id="parque3" name="parque" onclick="add(this);">GYM</a>
When you do the comparison and ask if it exists or not, if this element already exists in the
array
you simply remove it with.splice(posicion,nElementos)
.And you get that
posicion
of the element withindexOf(element);
more information here