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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    A href="#" doesn't work in document.write

    I want to show and hide div but i have a problem

    Code:
    <script type="text/javascript">
     function showstuff(boxid){
       document.getElementById(boxid).style.visibility="visible";
    }
     
    function hidestuff(boxid){
       document.getElementById(boxid).style.visibility="hidden";
    }
     </script>
    
    <div id="click_text_and_hide_me" style="visibility: visible">
    <a href="#" onclick="showstuff('selectionA');hidestuff('click_text_and_hide_me')">A</a><br>
    <a href="#" onclick="showstuff('selectionB');hidestuff('click_text_and_hide_me')">B</a></div>
    <div id="solution" style="visibility: hidden">Here is the solution</div>
    <div id="selectionA" style="visibility: hidden">
    You clicked the selection A<br>
    
    <script type="text/javascript">
    	var x="variable_inputed"; 
    	if( x === "variable_compare"){
    		document.write("<b>That's Right</b>");	
    	}else{ 
    		document.write("<b>False</b><br><a href="#" onclick="showstuff('solution')">Click here to see the solution</a>);
    		
    	}
    
    </script>
    </div>
    <==div selentionB is the same==>
    When I use
    Code:
    <a href="#" onclick="showstuff('solution')">Click here to see the solution</a>
    in document.write in the "IF ELSE" command. It doesn't work. so in "ELSE", How do I have to do with my aim? ( show div "solution" )
    Last edited by huyden181; 06-20-2012 at 10:44 AM.

  • #2
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    document.write("<b>False</b><br><a href="#" onclick="showstuff('solution')">Click here to see the solution</a>);

    You can see it's mismatch and wrong in many way. It should be like this.

    document.write("<b>False<\/b><br><a href=\"#\" onclick=\"showstuff(\'solution\')\">Click here to see the solution<\/a>");
    Last edited by Richter; 06-20-2012 at 11:02 AM.

  • Users who have thanked Richter for this post:

    huyden181 (06-20-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by huyden181 View Post
    <script type="text/javascript">
    var x="variable_inputed";
    if( x === "variable_compare"){
    document.write("<b>That's Right</b>");
    }else{
    document.write("<b>False</b><br><a href="#" onclick="showstuff('solution')">Click here to see the solution</a>);

    }

    </script>
    </div>
    <==div selentionB is the same==>
    [/CODE]
    This is meanigless. If var x="variable_inputed"; x cannot be equal to "variable_compare".

    document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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