Sudha
Sudha

Reputation: 2108

Apply style to selected text in a textbox

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.

Upvotes: 3

Views: 1441

Answers (2)

Sandy
Sandy

Reputation: 673

HERE IS THE COMPLETE SOLUTION...SUDHA

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

remove space between

Upvotes: 2

Mr Lister
Mr Lister

Reputation: 46559

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.

Upvotes: 0

Related Questions