Jump to content

[SOLVED] Populate a textarea by clicking link


bluesoul

Recommended Posts

Sorry for the possibly obscure working in the subject, not exactly sure the best way to describe it but I believe it's a fairly simple bit of JS.  I have a <textarea> that starts with some standard text.  I want to give my users some preset links that populate that textarea with some static data.  They can be simple text links or buttons.

Link to comment
Share on other sites

I feel like a schmuck but I'm not sure what to do with this.  I've messed with it a little bit and I guess I'm not calling the function the right way.

 

If the page loads with...

 

<textarea name="foo">This is foo text.</textarea>

 

And I want to click a link that says bar and have...

 

<textarea name="foo">This is bar text.</textarea>

 

Do I need to do something like...

 

<script type="text/javascript">
function addTxt(txt_content)
{
document.getElementById("foo").value = txt_content;
return false;
}
</script>
<a href="#" onClick="javascript:addTxt(This is bar text.)">BAR</a>

Link to comment
Share on other sites

do it like this:

 

<script type="text/javascript">
function addTxt(txt_content)
{
document.getElementById("foo").value = txt_content;
}
</script>

<textarea id="foo"></textarea><br><br>

<a href="javascript:addTxt('This Is Bar Text')">Bar</a>

 

Thanks, I figured out what was wrong (I gave the textarea a name but not an id).  Great stuff, you've helped more than you know!  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.