07 May, 2004
Change textarea text color with checkbox and javascript
Posted by: Jennifer In: Script snippet
Just want to store a little javascript snippet. I needed to grey out the text in a text area depending on if a checkbox was checked or not. I'm sure there's a way to get the function to be flexible enough so that it can be passed the specific parameters of WHICH form, WHICH checkbox, and WHICH textfield to grey out – but my usage was very limited, so I didn't bother going further.
Here's my function:
<script type="text/javascript">
function greyText() {
if (document.formname.showit.checked) {
document.formname.mytext.style.color = "#000";
} else {
document.formname.mytext.style.color = "#999";
}
}
</script>
And here's my form and fields:
<form name="formname">
<input name="showit" type="checkbox" value="" onclick="greyText()" /> Show this paragrah
<textarea name="mytext" wrap="virtual" style="color:#999;">Lorem ipsum dolor sit amet no nummy blah blah blah....</textarea>
</form>
Here it is in action: