+ Reply to Thread
Results 1 to 8 of 8

Thread: JavaScript replaceChild

  1. #1
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    JavaScript replaceChild

    I would like to use javascript to check if a certain html element is empty (I know it sounds ridiculous, just go with it :P)

    So this:
    Code:
    <p id="checkme"></p>
    Will be replaced with this:
    Code:
    <p id="checkme">There was no text here, but now there is!</p>
    I tried this, but it didn't work:
    Code:
    <p id="checkme"></p>
    <script type="text/javascript">
          var old = document.getElementById("checkme").firstChild; 
          var new = document.createTextNode("TEST"); 
          if(old.length == 0)
          { 
                document.getElementById("checkme").replaceChild(new, old);
          }
    </script>
    Any idea of how I can do this?

    The way I'm trying to do this requires the <script ...> directly before or after the <p> tag, although adding a script to the <head> is possible too.

    Thanks!
    Last edited by garrettroyce; 03-18-2009 at 08:43 PM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Test "node.childNodes.length". Use "appendChild()".

    If a node has no children, firstChild should be "null". You can also test node.childNodes.length.

    replaceChild(...) won't work on a node with no children because there are no children to replace. Use appendChild(...).

  3. #3
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: JavaScript replaceChild

    In the example case just a simple .innerHTML check should work:
    HTML Code:
    <p id="checkme"></p>
    <script type="text/javascript">
    if(!document.getElementById('checkme').innerHTML) document.getElementById('checkme').innerHTML = 'There was no text here, but now there is!'
    </script>
    This post has been marked spam 52 times.


  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript replaceChild

    Quote Originally Posted by t2t2t View Post
    In the example case just a simple .innerHTML check should work:
    True, but innerHTML isn't as future proof. On the other hand, it's usually more efficient, though the difference in efficiency won't be noticeable in the example. This isn't to say the OP shouldn't use innerHTML, zhe should just be sure to understand the consequences, good and bad. "Alternatives to innerHTML" and "The innerHTML dilemma" have more information.

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: JavaScript replaceChild

    I decided to go another route with this, thanks anyways!
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript replaceChild

    Out of curiosity (and for those reading this thread looking for help), what route did you take?

  7. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: JavaScript replaceChild

    Well, what I was trying to do is to develop a way to avoid the sometimes blank advertisement from using the X10 advertisement code. I know it's against the TOS to modify the code, but I thought maybe if I could detect that the code had not output anything, that I could then input my own message, like "check out x10hosting.com." I know it's possible to do, but I decided that the risk of my code malfunctioning and always removing the ad and the risk that this might be seen as a modification of the x10 ad code was too great.

    So, long story short, I simply did this:

    Code:
    <p><script src"x10 ad code blah blah blah /> -- Thank you sponsors!</p>
    It's easy, guaranteed to work, guaranteed to not get me in trouble, and only a little bit ugly :P

    EDIT:
    Sweet, X10 Lieutenant status and 1234 credits
    Last edited by garrettroyce; 03-25-2009 at 10:05 AM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript replaceChild

    Nice solution, actually.

    Unlike innerHTML, Using appendChild would mean a script generated ad would never be replaced, but I'd only use that approach if the extra content really bugged you or messed up your design.

+ Reply to Thread

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. A question about javascript files
    By rlodge in forum Programming Help
    Replies: 6
    Last Post: 12-19-2007, 11:26 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers