Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
click button write text
Old 08-31-2008, 02:44 AM click button write text
Extreme Talker

Posts: 176
Trades: 0
I have a <textarea>. I want one button that will insert text into it whereever the text cursor is at.

Also one that will insert text at the end of it.

I've been fooling around with it and looking at tutorials, but I can't seem to get it.
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-31-2008, 06:48 PM Re: click button write text
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
I've been fooling around with it and looking at tutorials, but I can't seem to get it
No it can be a real PITA to get working cross browser.

here's something I wrote earlier (I use it on my CMS system) complete with a bonus function to Prepend text as well as Insert (at cursor) and Append.

Code:
function insertText(p_oObj, p_sText) {
if (document.selection) {
p_oObj.focus();
var l_sSel = document.selection.createRange();
l_sSel.text = p_sText;
}
else if (p_oObj.selectionStart || p_oObj.selectionStart == '0') {
var l_iStartPos = p_oObj.selectionStart;
var l_iEndPos = p_oObj.selectionEnd;
p_oObj.value = p_oObj.value.substring(0, l_iStartPos)+ p_sText+ p_oObj.value.substring(l_iEndPos, p_oObj.value.length);
} else {
p_oObj.value += p_sText;
}
}

function appendText(p_oObj, p_sText) {
p_oObj.value += p_sText;
}

function prependText(p_oObj, p_sText) {
p_oObj.value =  p_sText + p_oObj.value;
}
demo buttons;
HTML Code:
<button onclick="insertText(document.getElementById('textarea'),' Some text to insert ')">Insert some text</button>

<button onclick="appendText(document.getElementById('textarea'),'  Some text to end ')">Append some text</button>

<button onclick="prependText(document.getElementById('textarea'),' Some text to front ')">Prepend some text</button>
Demo assumes the textarea has an ID of "textarea"
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to click button write text
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16053 seconds with 12 queries