Jump to content

txtarea tests


rarebit

Recommended Posts

Hi, can anyone test this with IE please? (or other browsers, i've tested with FF and Opera)

 


<script type="text/javascript">
function tester(s)
{
var txtbox = document.myform.post;
var begin = txtbox.value.substr(0, txtbox.selectionStart);
var end = txtbox.value.substr(txtbox.selectionEnd);
txtbox.value = begin + s + end;
document.myform.post.focus();
}
</script>

<a href='javascript:tester("world")'>tester</a><br>

<form name='myform'>
<textarea cols="64" rows="12" name="post">HELLO!</textarea>
</form>

 

Just click your cursor in the middle and click the link!

Link to comment
Share on other sites

Looks good, I would just add one line to the function to make it this:

function tester(s)
{
  var txtbox = document.myform.post;
  var begin = txtbox.value.substr(0, txtbox.selectionStart);
  var end = txtbox.value.substr(txtbox.selectionEnd);
  txtbox.value = begin + s + end;
  txtbox.selectionEnd = txtbox.selectionStart = begin.length + s.length;
  document.myform.post.focus();
}

Link to comment
Share on other sites

is this related to adding a text to a text area ??

 

I want for example when an event is triggered to write something where the txt cursor in the text area is pointing ..

 

for example like the buttons above ( B , I , ...) they add the bbcode where the cursor is pointing ..

 

please I need a clear example

 

thx

Link to comment
Share on other sites

That's what this does, but it doesn't wrap highlighted code, just replaces it or inserts it if nothing is selected.

 

If you wanted to wrap some text, the bit you want to wrap is the difference between 'begin' and 'end'. I haven't got access to my stuff right now but i'll post it soon...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.