473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bold the selected text in textarea for firefox

realin
254 100+
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.

[HTML]
if(document.selection){

var str=document.selection.createRange().text;
document.getElementById(txt).focus();
var sel=document.selection.createRange();
sel.text="<b>"+str+"</b>";


}

[/HTML]

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 :-
[HTML]

document.getElementById(txt).selectionStart;

document.getElementById(txt).selectionEnd;

document.getElementById(x).setSelectionRange(0,3); \\to select text
[/HTML]

please help
thanks :)
Aug 28 '07 #1
2 51592
phvfl
173 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. var $obj = document.getElementById("textarea1")
  2. alert($obj.value.substring($obj.selectionStart, $obj.selectionEnd))
  3.  
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:
Expand|Select|Wrap|Line Numbers
  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. }
  19.  

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
Aug 30 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1. var $obj = document.getElementById("textarea1")
  2. alert($obj.value.substring($obj.selectionStart, $obj.selectionEnd))
  3.  
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:
Expand|Select|Wrap|Line Numbers
  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. }
  19.  

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?
Dec 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Imaya Kumar | last post by:
Hi, i'm developing an editor in VS.NET using Rich Text Box Control. I need to make a selected text Bold. how can i do this? also i will have an Italic Button too. If i click Bold button the...
3
by: gooderthanyou | last post by:
I have a textfield and you of course you can select text... When they hit the bold button I want it to obtain the selected text and bold it, the hard part is trying to figure out if javascript...
4
by: Adnan Siddiqi | last post by:
Hi Pardon me if I am not making any sense.What I want to know the cordinates of selected text by user so that i can save/retrieve them later.is It possible.I am lookig for both IE and Firefox...
2
by: Daniel Pitts | last post by:
Why doesn't this work? I create an object which is supposed to handle the selection in both IE and Firefox, but everytime I call getText() in firefox, I get the whole textarea, not just the...
6
by: =?Utf-8?B?bWlrZQ==?= | last post by:
trying to convert selected text in a textbox to bold font. can someone provide me code sample? thanks.
2
by: King of the R.O.U.S.'s | last post by:
Hi All How do I replace selected text in a textarea with JavaScript? I have a text area that the user can select what they want then press a button that will pick up the selected text, make...
0
by: Ignacio Machin ( .NET/ C# MVP ) | last post by:
On Jul 22, 9:41 am, "none" <n...@none.comwrote: Take a look at the Selected property of the RichTextBox control
1
by: malathib | last post by:
Hi, I am trying to do the following in firefox.Please suggest the solution that will be applicable to firefox only. I am having one xml and xslt file. I kept the xml file content in iframe. Now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.