/    Sign up×
Community /Pin to ProfileBookmark

onClick in href to populate a text field

I’d like to populate a text field from an href

For example

<a href=”#” onclick=?>this text</a>

I’d like “this text to be populated into the text field.

Is this possible?

to post a comment
JavaScript

19 Comments(s)

Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
function appendText(s) { document.forms[0][0].value += s; }
//]]&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="#"&gt;
&lt;div&gt;
&lt;textarea rows="10" cols="50"&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#" onclick="appendText(firstChild.nodeValue);"&gt;foo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" onclick="appendText(firstChild.nodeValue);"&gt;bar&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — Thanks! That works, but the href is outside the form, and there are 3 other forms on the page... ?
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — [i]Originally posted by funkykangaroo [/i]

[B]Thanks! That works, but the href is outside the form, and there are 3 other forms on the page... [/B][/QUOTE]
No problem. The links can be anywhere, and the JavaScript could be easily modified to write to different (or multiple) elements. It doesn't only have to be the first one. Could you perhaps elaborate on what your requirements are?
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — Sure

I am dynamically creating a website link on a page. I'd like to be able to simply click on this link and have it populate into a certain text field in form "a"

Hope that explains it well enough
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — The statement:document.forms[0][0].value += s;Changes to:document.forms['a']['foo'].value += s; The string "foo" then changes to whatever the [font=courier]name[/font] of the form element is (of course, you could also use array notation or other such methods).
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — not working

getting a null or not not an object error
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — Can't really do much without seeing the exact code (JavaScript as well as markup) you're working with.
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — acutally, its working

but now I'm justt getting a "null" in the field
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — My previous comment still applies&hellip;
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — well, i think i can take it from here.. its something else that i'm not doing

you got me where i needed to be

thanks for your help!
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — No problem! ?
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — oops!

One more question!

What if I want the href link to just say "paste"

When you click on paste, it would populate the field with the correct URL

How would that work?
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — [i]Originally posted by funkykangaroo [/i]

[B]What if I want the href link to just say "paste"[/B][/QUOTE]
You would simply change the string contained within the opening and closing [font=courier]<a>[/font] tags which however, would seem to contradict your original purpose: to make it so the text within the link is written to a form element.[i]Originally posted by funkykangaroo [/i]

[B]When you click on paste, it would populate the field with the correct URL[/B][/QUOTE]
What "correct URL"? More detail needed.
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — i'm sorry for not being clear.. i've been coding for many hours

I didn't realize that i wanted just "paste" contained within the /a/ tag

When you mouseover the "paste" it would show the correct URL, which I want to be populated in the text field.

I'm sorry for being so unclear
Copy linkTweet thisAlerts:
@fredmvMar 12.2004 — Again &#8212; what is this "correct URL" you're referring to? The [font=courier]href[/font] of the link perhaps?
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — well its tricky because I don't want any action besides the population into the field, so the href is #

why dont i just give you some code!

<FORM METHOD=POST NAME=EditForm><span class='kupke2'>$MESS{'EditPage'}&nbsp;<BR></span> <INPUT TYPE="hidden" NAME="todo" VALUE="Edit"><INPUT TYPE="hidden" NAME="TheWord" VALUE="$in{'TheWord'}"><SELECT style="margin: 0px; width: 130px" name="Page"><option selected></option>

~;

foreach $_ (sort {uc($a) cmp uc($b)} (@files)) {

($Page,$HTML)=split(/|/);

if (length($Page) > 2) {
if ($Page eq "Home") { $temp = $temp."<option selected value=$Page>$Pagen" }
else { $temp = $temp."<option value=$Page>$Pagen" }
$ENV{'SCRIPT_NAME'} =~ /(.*)/.*/;

if ($ENV{'SERVER_NAME'}) {
$allpages = $allpages."<TR><TD valign=top width=80><A HREF="account.pl?$Page" target="_blank"><font color=#0000ff>$Page</font></A></TD><td width=50><a href=# onclick=appendText(firstChild.nodeValue);><font color=#000099>$Page</font></a></td><TD>http://$ENV{'SERVER_NAME'}$1/account.pl?$Page</TD><TR>n";
}
else {
$allpages = $allpages."<TR><TD valign=top width=80><A HREF="account.pl?$Page" target="_blank"><font color=#0000ff>$Page</font></A></TD><td width=50><a href=# onclick=appendText(firstChild.nodeValue);><font color=#008080>$Page</font></a></td><TD>http://$ENV{'REMOTE_ADDR'}$1/account.pl?$Page</TD><TR>n";
}
$PageNoUnderscore = $Page;
$PageNoUnderscore =~ s/_/ /g;
$JSlinks = $JSlinks."document.write('<OPTION VALUE="account.pl?$Page">$PageNoUnderscore')n";
}
}

print "$temp</SELECT>&nbsp;<input style="margin: 0px; margin-right: 15px" type="submit" value=" OK "></FORM>";
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — if that confuses me, trust me, it confuses me

i need sleep

any feedback is much appreciated
Copy linkTweet thisAlerts:
@funkykangarooauthorMar 12.2004 — I figured it out

Thanks for reading.. I need sleep
×

Success!

Help @funkykangaroo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...