Send onclick value to textarea


<input type="button" value="category" style="background-color:#ffffff;"
										onclick="(form.f_query.value = '{CATEGORY}')">
<input type="button" value="url" style="background-color:#ffffff;"
										onclick="(form.f_query.value = '{URL}')">

I want to do something similar to this, but instead of using buttons I want to use hyperlinks and I want to be able to click more than one hyperlink and have the textarea display all the clicked values.

Here’s one way to do it. Keep in mind that those ‘A’ elements will cause a page reload if Javascript is disabled.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>demo</title>
<script type='text/javascript'>

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()
{
  document.getElementById('ta').value += '{' + this.firstChild.nodeValue + '}\
';
  return false;
}
</script>
</head>
<body>

<div id='btns'>
<p><a href=''>category</a></p>
<p><a href=''>url</a></p>
</div>

<textarea id='ta' rows='10' cols='20'></textarea>

</body>
</html>

It works great in IE, but not in firefox.
Any suggestions to get this to work in firefox?

Works for me in FF 2.0

I just got a page reload in firefox. I am using version 1.5.0.9

Try this


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>demo</title>
<script type='text/javascript'>

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 + '}\
';
  xPreventDefault(e);
  return false;
}
function xPreventDefault(e)
{
  if (e && e.preventDefault) e.preventDefault();
  else if (window.event) window.event.returnValue = false;
}
</script>
</head>
<body>

<div id='btns'>
<p><a href=''>category</a></p>
<p><a href=''>url</a></p>
</div>

<textarea id='ta' rows='10' cols='20'></textarea>

</body>
</html>