3

I have the following code

   $('#txtEditor').select(function (e) {
        var start = e.target.selectionStart;
        var end = e.target.selectionEnd;
        selText = $('#txtEditor').val().substring(start, end);
    });

<asp:TextBox ID="txtEditor" runat="server" TextMode="MultiLine" Width="500px" Height="500px" Font-Size="Large"></asp:TextBox>

I want to apply some style (colour it or make it bold or ittallic etc) only to the selected text (after selection) in the textbox. In selText Im getting the selected string. But I couldn't find any event or function which will apply style only to the selected string in the textbox.

Sudha
  • 2,078
  • 6
  • 28
  • 53

2 Answers2

2

HERE IS THE COMPLETE SOLUTION...SUDHA

http:// js fiddle .net/ sandeepvirani/ mzays/

remove space between

Sandy
  • 663
  • 1
  • 4
  • 9
  • 1
    Ehm... why put the spaces in to begin with? – Mr Lister May 30 '13 at 19:06
  • @Sandeep Virani, I have tried this example. The thing is that its working for 'div' but never working for my 'textbox'. How can I modify the js file inorder to achieve my requirement? – Sudha Jun 03 '13 at 09:00
  • In my textbox the content (string) is typing dynamically and I want to style a selected portion. (Like what we can do in MSWord or something) – Sudha Jun 03 '13 at 09:04
  • @Mr Lister : stackoverflow can't allow me to put link of jsfiddle thats why. – Sandy Jun 05 '13 at 11:59
  • I didnt get a solution yet. If its in a div its fine. But I couldn't get it done with a textbox. – Sudha Jun 06 '13 at 10:05
0

To change the color of the selection part of a text on a page, use the CSS ::selection pseudo-element.

See MDN documentation.

Note however, that

  • This is a non-standard selector, which is supported by many browsers, but not endorsed by the W3C.
  • You can't change just any property, really only just colors. Not bold!

If you want to change some text inside a paragraph or something, you can use Javascript to quickly put the whole of the selection inside a new element, a span or something, and then apply properties to the span. However, if it's text in a textbox, you're out of luck, as you can't add child elements to an input.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150