Samuel Ramzan
Samuel Ramzan

Reputation: 1856

Copy from list to textarea but prevent duplicates

I have this code, it helps to select values from a list and pass them to a textarea but, I don't want to duplicate in case accidentally pressing the same list value.

Heres is what i got:

window.onload = btnsInit;
function btnsInit()
{
  var i, a = document.getElementById('btns').getElementsByTagName('a');
  for (i = 0; i < a.length; ++i) {
    a[i].onclick = btnClick;
  }
}
function btnClick(e)
{
  document.getElementById('ta').value += '' + this.firstChild.nodeValue + '\n';
  xPreventDefault(e);
  trim();
  return false;
}
function xPreventDefault(e)
{
  if (e && e.preventDefault) e.preventDefault();
  else if (window.event) window.event.returnValue = false;
}

Any idea anyone?

Thank's

Upvotes: 0

Views: 136

Answers (1)

ted
ted

Reputation: 5329

Have you tried any checkings?

function btnClick(e)
{     
  if ( (document.getElementById('ta').value).indexOf(this.firstChild.nodeValue) ) < 0
      document.getElementById('ta').value += '' + this.firstChild.nodeValue + '\n';
  xPreventDefault(e);
  trim();
  return false;
} // haven't tested;

// FOR DEBUG PURPOSE

function btnClick(e)
{     
  console.log((document.getElementById('ta').value).indexOf(this.firstChild.nodeValue));
  console.log("ta =", document.getElementById('ta').value);
  console.log("value =", this.firstChild.nodeValue);
  if ( (document.getElementById('ta').value).indexOf(this.firstChild.nodeValue) ) < 0
      document.getElementById('ta').value += '' + this.firstChild.nodeValue + '\n';
  xPreventDefault(e);
  trim();
  console.log("#end ta =", document.getElementById('ta').value);
  return false;
} // haven't tested;

Upvotes: 2

Related Questions