Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

bold the selected text in textarea for firefox

Question posted by: realin (Familiar Sight) on August 28th, 2007 05:49 PM
hi guys,

I am trying to make a simple text editor for IE and firefox using javascript. I want to know how can i bold the selected text and otehr stuff like italics and all. cause in IE i was able to do with the following lines.

Code: ( text )
  1. if(document.selection){
  2.  
  3.     var str=document.selection.createRange().text;
  4.     document.getElementById(txt).focus();
  5.     var sel=document.selection.createRange();
  6.     sel.text="<b>"+str+"</b>";
  7.    
  8.    
  9.     }


But i what i am able to get for firefox is how to select the text, and how to set position of the caret using the following functions :-
Code: ( text )
  1. document.getElementById(txt).selectionStart;
  2.  
  3. document.getElementById(txt).selectionEnd;
  4.  
  5. document.getElementById(x).setSelectionRange(0,3);   \\to select text


please help
thanks :)
Last edited by realin : August 28th, 2007 at 05:49 PM. Reason: Email notification
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
phvfl's Avatar
phvfl
Expert
142 Posts
August 30th, 2007
05:39 PM
#2

Re: bold the selected text in textarea for firefox
Hi,

The document.selection method is IE specific and so does not work in other browsers. The method is slightly more involved in non-IE browsers, assuming that your textarea had the id "textarea1" (original I know :)) this code would give an alert with the selected text:
Code: ( text )
  1. var $obj = document.getElementById("textarea1")
  2. alert($obj.value.substring($obj.selectionStart, $obj.selectionEnd))


The selectionStart and selectionEnd properties give the positions for the start of the selection and the end, which are then used to get the selection from the value of the textbox.

To reproduce the current behaviour in IE across browsers try:
Code: ( text )
  1. function makeitbold(){
  2.   var $tb = document.getElementById("textbox");
  3.  
  4.   if (document.selection){
  5.     var str=document.selection.createRange().text;
  6.     var sel=document.selection.createRange();
  7.     sel.text="<b>"+str+"</b>";
  8.   }else if (typeof $tb.selectionStart != 'undefined'){
  9.     var $before, $after, $selection;
  10.     $before= $tb.value.substring(0, $tb.selectionStart)
  11.     $selection = $tb.value.substring($tb.selectionStart, $tb.selectionEnd)
  12.     $after = $tb.value.substring($tb.selectionEnd, $tb.value.length)
  13.    
  14.     $tb.value= String.concat($before, "<b>", $selection, "</b>", $after)
  15.   }
  16.    $tb.focus();
  17.  
  18. }



I have selected the textarea into the variable $tb as it reduces the code required. This performs the same accross browsers except for one instance. When no text is selected in the textbox clicking a button to run the function behaves differently:
  • IE - adds <b></b> to the button text
  • Non-IE - adds <b></b> to the end of the textarea

Hope this covers everything, let me know if any of this is unclear

Reply
veseffi's Avatar
veseffi
Newbie
1 Posts
December 26th, 2007
06:16 AM
#3

Re: bold the selected text in textarea for firefox
Quote:
Originally Posted by phvfl
Hi,

The document.selection method is IE specific and so does not work in other browsers. The method is slightly more involved in non-IE browsers, assuming that your textarea had the id "textarea1" (original I know :)) this code would give an alert with the selected text:
Code: ( text )
  1. var $obj = document.getElementById("textarea1")
  2. alert($obj.value.substring($obj.selectionStart, $obj.selectionEnd))


The selectionStart and selectionEnd properties give the positions for the start of the selection and the end, which are then used to get the selection from the value of the textbox.

To reproduce the current behaviour in IE across browsers try:
Code: ( text )
  1. function makeitbold(){
  2.   var $tb = document.getElementById("textbox");
  3.  
  4.   if (document.selection){
  5.     var str=document.selection.createRange().text;
  6.     var sel=document.selection.createRange();
  7.     sel.text="<b>"+str+"</b>";
  8.   }else if (typeof $tb.selectionStart != 'undefined'){
  9.     var $before, $after, $selection;
  10.     $before= $tb.value.substring(0, $tb.selectionStart)
  11.     $selection = $tb.value.substring($tb.selectionStart, $tb.selectionEnd)
  12.     $after = $tb.value.substring($tb.selectionEnd, $tb.value.length)
  13.    
  14.     $tb.value= String.concat($before, "<b>", $selection, "</b>", $after)
  15.   }
  16.    $tb.focus();
  17.  
  18. }



I have selected the textarea into the variable $tb as it reduces the code required. This performs the same accross browsers except for one instance. When no text is selected in the textbox clicking a button to run the function behaves differently:
  • IE - adds <b></b> to the button text
  • Non-IE - adds <b></b> to the end of the textarea

Hope this covers everything, let me know if any of this is unclear

Hi,
The code works in IE for Textareas,divs and spans, but only in a textarea in FireFox. I want the same code to work in IE and Firefox for normal text (like in a div or a span) such that onclick of a button, the selected text should come as alert message. could you please post the code for that?

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,043 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors