javascript add text into a form
Moderator: General Moderators
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
javascript add text into a form
Like the emoticons on this form, how can i use javascript to add something like Clicky smiles?
well, phpBB does it this wayif you open the "more emoticons"-window the code isthe only difference is the usage of the opener-element which refers to the opening window/frame
Code: Select all
...
function emoticon(text) {
text = ' ' + text + ' ';
if (document.post.message.createTextRange && document.post.message.caretPos) {
var caretPos = document.post.message.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
document.post.message.focus();
} else {
document.post.message.value += text;
document.post.message.focus();
}
}
...
<textarea name="message" rows="15" cols="35" wrap="virtual" tyle="width:450px" tabindex="3" class="post" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);"></textarea>Code: Select all
function emoticon(text) {
text = ' ' + text + ' ';
if (opener.document.formsї'post'].message.createTextRange && opener.document.formsї'post'].message.caretPos) {
var caretPos = opener.document.formsї'post'].message.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
opener.document.formsї'post'].message.focus();
} else {
opener.document.formsї'post'].message.value += text;
opener.document.formsї'post'].message.focus();
}- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
Ok, Looked at the code for the way phpBB does it, and some other scripts, and this seems to work:
Took me a while to see that you have to have <form name="post"> and <textarea name="message">. I dont know any javascript.
Code: Select all
<script language="Javascript" type="text/javascript">
<!--
function insert(text) {
text = ' ' + text + ' ';
if (document.post.message.createTextRange && document.post.message.caretPos) {
var caretPos = document.post.message.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
document.post.message.focus();
} else {
document.post.message.value += text;
document.post.message.focus();
}
}
//-->
</script>
<a href="javascript:insert(':)')">SMILE</a><p>
<form name="post" action="next.html" method="POST">
<textarea name="message" rows="15" cols="35" wrap="virtual" style="width:450px" tabindex="3" class="post"></textarea>
</form>