Javascript OnClick Hyperlink to Populate Form Field?

First off, I want to apoloigze if this has already been answered … the SP search feature is taking about 2 mins per search, so it was making it difficult to search the archive …

Anyways, on to my question …

I have a form such as:

<form name="additem" method="post" enctype="multipart/form-data" action="index.php">
<input name="itemname" type="text" id="itemname" size="40">
</form>

And I’m wanting to be able to click on a hyperlink to populate the “itemname” input field … such as

<a href="#" onclick="CopyDetails('HERE IS THE TEXT I WANT COPIED',additem,itemname)">Copy to form</a>

I know CopyDetails is not a JS function, but it was just part of the example I came up with.

The hyperlinks that I will be clicking will be on the same page as the form.

Any thoughts or ideas?

  • Scott
function init() {
  document.getElementById('copydetails').onclick = copyDetails('Copy this text', 'itemname');
  // other things to do onload
}

function copyDetails(text, field) {
  document.getElementById(field).value = text;
}

window.onload = init;

Not necessary to have all those functions if this is the only javascript you need, but I’m guessing you might need to populate other form fields, so this should get you started. Also, there’s no need for messy inline Javascript in the HTML, so your link would just be:

<a href="#" id="copydetails">Copy to form</a>

Raffles …

You Rock!


<script language="javascript/text">
function addTxt(txt, field)
{
var myTxt = txt;
var id = field;
document.getElementById(id).value = myTxt;
}
</script>


<p><a href="#" onclick="addTxt('this is some text!', 'info')">Click here to add Text</a></p>
<form name="test">
<input type="text" id="info" value="">
</form>

Well dang, a little too late.