/    Sign up×
Community /Pin to ProfileBookmark

textarea default value clear

hi. i just started to learn javascript. and i was wondering how i could clear the value on the textarea by clicking on it.

i know what i can use:

onclick=”function_name”

and then i can make a function that does something with document.write and so on, but i dont know how i should put it all toghterer, what should be inside the value=”” inside the text area? should i mae a <script></script> inside of the value=”” like we do with php value=”<?php echo somefunction(); ?>”

regards,
alex

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — is there a way to check if the onclick() even occured? just like the php version of isset() to check if a submit button was pressed. or is it a better and more elegant way of doing all this?
Copy linkTweet thisAlerts:
@tirnaMar 14.2010 — I wouldn't make it so that clicking the textarea clears it just in case a user accidentally clicks inside it after entering text. But if you really want it that way add the following onclick event handler to the textarea opening tag.

[CODE]
<textarea onclick="this.value=''; ">
[/CODE]


Another way of clearing the textarea is to click a separate button as per the html below


[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
<head>
<title></title>
<script type="text/javascript">
function cleatTextAtea() {
document.getElementById("txtArea").value='';
}
</script>

</head>
<body>
<textarea id="txtArea" cols="40" rows="5">Enter text here....</textarea>
<button onclick="cleatTextAtea();">Clear Textarea</button>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — isnt it a way to clear text just the first time someone clicks on it?
Copy linkTweet thisAlerts:
@tirnaMar 14.2010 — This should do the trick to clear the textarea only on the first click on it

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
var count = 1

function cleatTextAtea(elem) {
if(count == 1) {
elem.value='';
count = count + 1;
}
}
</script>

</head>
<body>
<textarea onclick="cleatTextAtea(this)" cols="40" rows="5">Enter text here....</textarea>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — thanks i will try it ?
Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — hm this does not work, doesnt seem like the variable is getting updated.

it cleares more than once
Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — oh nevermind it works^^ thanks!
Copy linkTweet thisAlerts:
@inseiauthorMar 14.2010 — found this on http://javascriptkit.com

nice code in case someone else needs it like i did.

[CODE]
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
[/CODE]
×

Success!

Help @insei 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.13,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...