Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: InnerHTML, better support.

  1. #1
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default InnerHTML, better support.

    I am using this code, what it does is when a user clicks a link it waits 6000 msecs and then the the content of DIV1 is the same as DIV2.

    Code:
    <script type="text/javascript">
    <!--
    
    function timer(){
    setTimeout('contentchanger()',6000);
    }
    function contentchanger() {
    DIV1.innerHTML=DIV2.innerHTML
    }
    // -->
    </script>
    <a href="#" onclick="timer()">Click</a><br />
    <div id="div1">When clicked, this text will not show</div>
    <div id=div2">After clicking this should be the content of both divs</div>
    However it only works with IE. Can someone help me rewrite it so it works cross browser.

    Thanks
    Last edited by Tabo; 04-28-2007 at 09:53 AM.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Can you post us your full code please?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tech_support View Post
    Can you post us your full code please?
    Done in the above.

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Well, what's DIV1 and DIV2?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Try this
    Code:
    <script type="text/javascript">
    <!--
    
    function timer(){
    setTimeout(function(){contentchanger()},6000);
    }
    
    function contentchanger() {
    DIV1 = document.getElementById("div1");
    DIV2 = document.getElementById("div2");
    DIV1.innerHTML = DIV2.innerHTML;
    }
    // -->
    </script>
    <a href="Javascript:timer()">Click</a>
    <br />
    <div id="div1">When clicked, this text will not show</div>
    <div id="div2">After clicking this should be the content of both divs</div>
    Last edited by Bob90; 04-28-2007 at 12:07 PM.

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Use document.getElementById();
    Code:
    <script type="text/javascript">
    function timer(){
    setTimeout(function(){
      var DIV1 = document.getElementById("div1");
      var DIV2 = document.getElementById("div2");
      DIV1.innerHTML = DIV2.innerHTML;
      },6000);
    }
    </script>
    <a href="javascript:timer()">Click</a>
    <br />
    <div id="div1">When clicked, this text will not show</div>
    <div id="div2">After clicking this should be the content of both divs</div>
    The "<--" and "-->" are pointless, JavaScript won't be used anyways for browsers that can't read it.
    Also, declaring multiple functions for one task is inefficient.
    - Mike

  7. #7
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Its missing a }

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It's not, actually.
    - Mike

  9. #9
    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

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function timer(){
    setTimeout('contentchanger()',6000);
    return false;
    }
    function contentchanger() {
    var d1=document.getElementById? document.getElementById('div1') : document.all['div1'],
    d2=document.getElementById? document.getElementById('div2') : document.all['div2'];
    d1.innerHTML=d2.innerHTML;
    }
    
    </script>
    </head>
    <body>
    <div>
    <a href="#" onclick="return timer();">Click</a><br>
    <div id="div1">When clicked, this text will not show</div>
    <div id="div2">After clicking this should be the content of both divs</div>
    </div>
    </body>
    </html>
    - John
    ________________________

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

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    What's the point of that? Internet Explorer supports document.getElementById().
    - Mike

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
  •