Results 1 to 8 of 8

Thread: Coding Challenge: Change innerText without changing its style.

  1. #1
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Coding Challenge: Change innerText without changing its style.

    How can we change some element's innerText without changing the innerText's style?
    I need to do this:
    There's a div on the page. Inside the div, there's just text, but with different fonts. E.g. aaaaaaaaaaaaaaabbbbbbbbbbccccccccccccc
    And there's a input field, shows the content of the div. A user can change the stuff inside the input field then the div will change accordingly. Say if someone replace all the a's with d's, the div will appear as:
    ddddddddddddddbbbbbbbbbbccccccccccccc
    How can I make this happen?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Only change the text.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Don't use innerText, it only exists in IE.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I didn't think there was such a thing as innerText. Anyways, my suggestion should work, if you set a variable to the innerHTML of the overall containing element, then work whatever changes you wish upon it as a variable, and then assign it as the overall containing element's innerHTML.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This could be one of the rare situations where the DOM methods are simpler to use, if you wish to keep the fonts constant.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Could be.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As long as you do not delete or backspace over where the unseen font tags are. This works in IE and Opera9, not in FF:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    #theTxt {
    border:1px solid gray;
    width:400px;
    }
    </style>
    <script type="text/javascript">
    function loadTxt(){
    document.getElementById('theTxt').innerHTML=document.getElementById('theDiv').innerHTML//.replace(/<[^>]*>/g, '')
    }
    function loadDiv(){
    document.getElementById('theDiv').innerHTML=document.getElementById('theTxt').innerHTML
    }
    onload=loadTxt;
    </script>
    </head>
    <body>
    <div id="theDiv"><font face="arial">aaaaaaaaaaaaaaa</font><font face="serif">bbbbbbbbbb</font><font face="arial">ccccccccccccc</font></div>
    <div contenteditable id="theTxt"></div>
    <input type="button" onclick="loadDiv()" value="Update">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks heaps John! That works all fine. Thanks everyone for your time.

Bookmarks

Posting Permissions

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