Results 1 to 8 of 8

Thread: JavaScript: Replacing Text

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140

    JavaScript: Replacing Text

    Okay, this code doesn't work.

    Code:
    <html>
    
      <head>
        <script type="text/javascript">
          function SetText(myText) {
            var TargetEl = document.getElementById("TextBlock");
            TargetEl.replaceWholeText(myText);
          }
    
          var TestText = "Some test text.";
        </script>
      </head>
    
      <body>
    
        <p id="TextBlock">TSLIB</p>
    
        <p><a href="JavaScript:SetText(TestText);">Some test text.</a></p>
    
      </body>
    
    </html>
    First, don't complain about the use of the anchor tag. I'm not too keen on using it either, but it works.

    Second, I'm sure there is a better way to do this, but I'm trying to understand this method. If you have a different way, I'm interested, but I want to make this way work, too.

    Now, the replaceWholeText method is part of the Text object. At this point, TargetEl is an Element Object. So how does name space work? How can I grab this element/node in such a way that I can replace it's text?

    Documentation helps, thanks.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Leave it to me to ask the question no one can answer...

    My problem is, there is no good documentation.

    I am finding createTextNode and appendChild and replaceChild, which sounds like a good strategy, but they deal with different objects: Element, Node, Text.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Ah, got it.

    Element and Text objects are just types of Node objects. I think...

    Code:
    <html>
    
      <head>
    
        <script type="text/javascript">
    
          function SetText(myText) {
            var TextBlockEl = document.getElementById("TextBlock");
            var NewText = document.createTextNode(myText);
            TextBlockEl.replaceChild(NewText, TextBlockEl.childNodes[0]);
          }
    
        </script>
    
      </head>
    
      <body>
    
        <p id="TextBlock">TSLIB</p>
    
        <p><a href="JavaScript:SetText('Some test text.');">Some test text.</a></p>
    
      </body>
    
    </html>
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Did you try just replacing the SetText(TEstText) with SetText("TestText") ?

    IU noticesd you did that eventually, but maybe that was the only original prob?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  5. #5
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    thats IE only right?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    No, Sail, it is complaint with the DOM model and ECMA's mapping. If your browser has a problem with the code, then it is not a complaint browser.

    In which case, I couldn't care less.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  7. #7
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    LOL Me too. Basically all i work for now is IE and NS6 (little opera too). Opera has some problems with it's DOM.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Yes, Opera has problems with anything dynamic. And I don't understand. I really really really want to ask the guys at Opera what in the hell are they smoking?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

Posting Permissions

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



Click Here to Expand Forum to Full Width