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
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Prevent My Content From Disappearing

    How do I prevent my Link from disappearing??
    When I click on the link, "Click Here"

    It display, "Look At Me!!" but the link, "Click Here" is GONE
    Is there a way to keep my link, "Click Here" from disappearing?
    So when I click on the link, "Click Here" the content, "Look At Me!!"should display as well.

    thanks


    Here are my codes
    Code:
    <html>
    <head>
    
    <script type="text/javascript"> 
    function display() {
       document.writeln("Look At Me!!");
    }
    </script>
    </head>
    
    <body>
    <a href="google.com" onClick="display()">Click Here</a>
    </body>
    
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Once a page is completely loaded, *ANY* calls to document.write will *WIPE OUT ALL THE CONTENTS* of that page, including even the JavaScript that did the document.write.

    The *ONLY* time you can successfully use document.write is during the initial creation of a page.

    You need to learn about DOM methods.

    Many many many ways to do this.

    *ONE* possible way:
    Code:
    <html>
    <head>
    
    <script type="text/javascript"> 
    function display() {
       document.getElementById("displayGoesHere").innerHTML = "Look At Me!!";
    }
    </script>
    </head>
    
    <body>
    <a href="google.com" onClick="display()">Click Here</a>
    <div id="displayGoesHere"></div>
    </body>
    </html>
    But that's just one of many.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts
    .innerHTML is used when you trigger an event but if you just want to display the text how would you go about do it??
    I don't want to used document.writeln because it will "Wipe out all the contents" of that page.
    So I have some HTML text and javascript text. I would like both to display on my page.

    Here's my code

    Code:
    <html>
    <head>
    
    <script type="text/javascript"> 
    function display() {
       document.writeln("How do I make both text appear");
    }
    </script>
    </head>
    
    <body onload="display()">
    I would like this text in the body to remain.
    
    <script type="text/javascript">
       display();
    </script>
    
    
    </body>
    </html>
    thanks

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    Code:
    <html>
    <head>
    
    <script type="text/javascript"> 
    function display() {
       document.getElementById("displayGoesHere").innerHTML = "Look At Me!!";
    }
    </script>
    </head>
    
    <body>
    <a href="javascript:void 0" onClick="display(); return false";>Click Here</a>
    <div>I would like this text in the body to remain.</div>
    <div id="displayGoesHere"></div>
    </body>
    </html>
    Last edited by xelawho; 09-17-2011 at 03:58 PM.

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <a href onclick="display(); return false";>Click Here</a>
    If I was to take this part of the code out this will not work but how can I make it work without the Click Here link

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts


    you need something to fire the display() function or your text will not show.

    you could fire it when the document loads, but that would be kind of pointless - you might as well just write it directly in like the "I would like this text in the body to remain" text.

    there's any number of ways that a user can fire a function (and in this case show the text) - buttons, checkboxes, radio buttons, etc, plus some others involving timers and event listeners.

    it all depends on what you want to do...


  •  

    Posting Permissions

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