how to get the selected value in textbox and change its value? how to do it in javascript?
This is what i am after. I was working on a bbcode editor and i wanted to get the the selected text in the textarea and wanted to insert bbcode tags in place of them. I needed just very simple javascript code an finally i found a solution working in both IE and Firefox.
// code for IE
var textarea = document.getElementById("textarea");
if (document.selection)
{
textarea.focus();
var sel = document.selection.createRange();
// alert the selected text in textarea
alert(sel.text);
// Finally replace the value of the selected text with this new replacement one
sel.text = '<b>' + sel.text + '</b>';
}
The above code only works in IE versions. You will need a different javascript code that works in Firefox. The document.selection will be true if it is IE else it would return false.
In Mozilla, you have to first get the length, starting point of cursor and ending point of highlighted cursor. While doing replacement you have first output the textarea value from substr(0,Startingpoint) + replacement + substr(Startingpoint,Length). This is the trick to change or replace the selected text in the textarea.
// code for Mozilla
var textarea = document.getElementById("textarea");
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
// This is the selected text and alert it
alert(sel);
var replace = '<b>' + sel + '<b>';
// Here we are replacing the selected text with this one
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
Just thought that i could share my little work in my blog.
Similar Posts:
- Fix -> Caret/Cursor jumps to start of textarea in javascript
- iframe innerHTML value from textarea
- how to hide textarea?
- Lightweight PHP WYSIWYG HTML Editor
- Free Markup TextArea HTML Editor in Javascript
- A Simple WYSIWYG Editor in Javascript
- Top Best Web based WYSIWYG html editors (Free)
- Free Markup BBCode Editor in Javascript
- PHP script to convert text string to image
- Free BBCode/HTML Markup Editors!


November 5, 2008
So your code “works” in that it updates the value of my text field. What it does NOT do is actually make the text bold (in a standard textarea). Is it possible to format only a portion of the text in a standard textarea field? By format I mean make it bold, or color the text or something?
November 5, 2008
That is the problem with textarea. It does not support rich text formatting. You have to use iframe make it bold or other formatting.
January 6, 2010
Het there, I think you’ll call me god when you’ll see this create a free flash website
March 12, 2010
Thanks, dude. It has solved the problem that I’m tackling with tiring several continous hours of work. It didn’t work out first, but then I found out that I made misspelling in code (you know after many hours of work, our performance lowers). then it worked out wonderfully. thanks again.