Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts

    How to make a text bold by highlighting?

    Hello, I am making a small application in JQuery. I want to know, how to make a text bold by highlighting it? For example, like those online text editors or WYSIWYG textarea?

    Suppose I havea button, once clicked on it, it will write "<strong></strong> but I want that when the user is selecting or highlight the sentence or word with mouse, then click on the button, it apply the Bold effect...

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    I have played with this for a couple of days now. In order to play with styles we need to use the ::selection selector in css3 and that can only change the color, background, cursor, and outline styles.
    This will change colors, but not font-weight.
    Code:
    <style type="text/css">
    ::-moz-selection{
    background:#C1EEFB ;color: red;}
    ::selection{
    background:#C1EEFB; color: red;
    }
    </style>

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts
    well i got my answer, you should do it with "contenteditable" feature in HTML5, then use document.execCommand in JavaScript!

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    Correct me if I'm wrong but doesn't document.execCommand ONLY work in IE and is therefore not a real world solution?

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Nope it works for all, search it... The only difference is that it does not work the same as other browsers..

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    OK I found it at https://developer.mozilla.org/en-US/...uting_Commands

    But I am having one hell of a time trying to get it to work. I'm using FF.
    Could you give us a small snipit of code to bold selected text?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •