www.webdeveloper.com
Results 1 to 5 of 5

Thread: Simple Show/Hide Button

  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Simple Show/Hide Button

    Hi. Sorry but i'm not english so i can make some grammar mistakes. I've written this code:
    HTML Code:
    <?xml version="1.0" encoding="utf-8"?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"> 
    <head> 
    	<title>Lirians</title> 
    	<script type="text/javascript" charset="UTF-8" src="skrypty.js"></script>
    	<script type="text/javascript" charset="UTF-8" src="jquery-1.3.2.js"></script>
    	<script type="text/javascript" src="http://koval.ovh.org/skrypty/getebc.js"> </script>
    </head> 
    <body>
    	
    	<div id="spany"> 
    		<span class="ukryj">Ukryj Tekst</span><br />
    		<span>Lorum Ipsum </span>
                    <span class="ukryj">Ukryj Tekst</span><br />
    		<span>Lorum Ipsum </span>
    	</div>
    	
    	<script type="text/javascript" charset="UTF-8">
    		document.getElementsByClassName('ukryj').onclick = ukrywaj;
    		
    		function ukrywaj() {
    			var zmienna = this.nextSibling.nextSibling.style.display;
    			if (zmienna == 'inline') {zmienna = 'none';}
    			else {zmienna = 'inline';}
    		}
    	</script>
    
    </body>
    </html>
    My question is:
    - Code is wrong because of "display of undefined". I have span, br and span so first span is like a button and it has to show or hide a next span. I'm beginner but it seems to be a simple mistake. Please help.

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,682
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
    <head>
    	<title>Lirians</title>
    	<script type="text/javascript" charset="UTF-8" src="skrypty.js"></script>
    	<script type="text/javascript" charset="UTF-8" src="jquery-1.3.2.js"></script>
    	<script type="text/javascript" src="http://koval.ovh.org/skrypty/getebc.js"> </script>
    </head>
    <body>
    
    	<div id="spany">
    		<span id="ukryj0">Ukryj Tekst</span><br />
    		<span>Lorum Ipsum </span>
                    <span id="ukryj1">Ukryj Tekst</span><br />
    		<span>Lorum Ipsum </span>
    	</div>
    
    	<script type="text/javascript" charset="UTF-8">
    //		not all browsers support document.getElementsByClassName
     for (var z0=0;document.getElementById('ukryj'+z0);z0++){
      document.getElementById('ukryj'+z0).onclick = ukrywaj;
     }
    
    		function ukrywaj() {
    			var zmienna = this.nextSibling.nextSibling;
                zmienna.style.display=zmienna.style.display!='none'?'none':'inline';
    		}
    	</script>
    
    </body>
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Sep 2012
    Posts
    4
    It doesn't work. I'm using Chrome and there is:
    "Uncaught TypeError: Cannot read property 'display' of undefined"

  4. #4
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,682
    that is because you have white space between the <br> and span

    use HTML

    Code:
    	<div id="spany">
    		<span id="ukryj0">Ukryj Tekst</span><br /><span>Lorum Ipsum </span>
            <span id="ukryj1">Ukryj Tekst</span><br /><span>Lorum Ipsum </span>
    	</div>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  5. #5
    Join Date
    Sep 2012
    Posts
    4
    You don't know how much i'm happy that it's working right now. I've forgotten about whitespace Thx

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles