/    Sign up×
Community /Pin to ProfileBookmark

Filling in a <textarea> with a function

I’m not good at all with javascript, and i try to avoid it as much as possible, however in the php script i’m working on i need it.
the script creates an html page and the page has a <textarea>

what i need is a javascript that will create a function called “num” and it would insert text from the html page that a variable in the php ($no) adds to the html page, into <textarea> which is called “com”

so i need some help with that.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledMar 26.2009 — it don't make sense to me. you can put the content in textarea directly in php. probably something like this:
[code=php]
echo "<textarea>$no</textarea>";
[/code]

or you want it under certain circumstance? if so, when or which circumstance?
Copy linkTweet thisAlerts:
@mike939393authorMar 26.2009 — 
or you want it under certain circumstance? if so, when or which circumstance?[/QUOTE]


i want to activate the function in an <a href=>
Copy linkTweet thisAlerts:
@ZeroKilledMar 26.2009 — assuming that the html element have an id, you would refer to the element and grab the content. the [B]bold[/B] code can vary according to how many elements have the same name:
<i>
</i>&lt;script type='text/javascript'&gt;
function num(){
[B]document.getElementsByName('num')[0][/B].value = document.getElementById('phpcontent').innerHTML;
}
&lt;/script&gt;
&lt;div id='phpcontent'&gt;here will go the content&lt;/div&gt;
&lt;a href='#' onclick='num(); return false;'&gt;link&lt;/a&gt;
&lt;textarea name='num'&gt;&lt;/textarea&gt;
Copy linkTweet thisAlerts:
@mike939393authorMar 26.2009 — thanks alot ?
Copy linkTweet thisAlerts:
@mike939393authorMar 26.2009 — i ran into a problem, in the html page, there are multiple instances of $no and each one is different (1,2,3), however, when i click them, it only adds what the first $no is to <textarea>, any help?
Copy linkTweet thisAlerts:
@ZeroKilledMar 26.2009 — it mean you need/have different link for each content:
<i>
</i>&lt;script type='text/javascript'&gt;
function num(id){
document.getElementsByName('num')[0].value = document.getElementById(id).innerHTML;
}
&lt;/script&gt;
&lt;div id='phpcontent'&gt;here will go the content&lt;/div&gt;
&lt;a href='#' onclick='num("phpcontent1"); return false;'&gt;link1&lt;/a&gt;
&lt;a href='#' onclick='num("phpcontent2"); return false;'&gt;link2&lt;/a&gt;
&lt;a href='#' onclick='num("phpcontent3"); return false;'&gt;link3&lt;/a&gt;
... so on ...
&lt;textarea name='num'&gt;&lt;/textarea&gt;


on each call you pass the element's id according to which you want to refer. notice that each ID have to be unique.
Copy linkTweet thisAlerts:
@mike939393authorMar 26.2009 — here's what i have
<i>
</i>&lt;a href="#" onclick="num('phpcontent$no'); return false;"&gt;&lt;span id='phpcontent'&gt;$no&lt;/span&gt;&lt;/a&gt;

and the <textarea> is num

however when i click it i just get returned to the top of the page.

[b]edit:[/b] nevermind i fixed it, thanks alot ?
×

Success!

Help @mike939393 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 5.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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