SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some show/hide text help

    I have the following code:

    Code:
    function toggle( targetId ){
      if (document.getElementById){
    			
    		  target = document.getElementById( targetId );
    			  if (target.style.display == "none"){
    				  target.style.display = "";
    
    			  } else {
    				  target.style.display = "none";
    			  }
    	  }
    }
    In a nutshell, I have the following link:

    <a href="#" class="more" onclick="toggle('sub_section') ;return false;">Other uses of the word Golf</a>

    I have a div with id "sub_section" that properly shows/hides when clicked, which is what I indended.

    however, I want to make it so when the div is hidden, the link says

    "Show other uses of the word Golf"

    and when it is showing, I want the link to say

    "Hide other uses of the word Golf"

    How can I do this? can someone alter my code to do this?

    Thanks

  2. #2
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    function toggle(aRef,targetId ){
      if (document.getElementById){
    
    		  target = document.getElementById( targetId );
    			  if (target.style.display == "none"){
    				  target.style.display = "";
                      //change text
                       aRef.firstChild.nodeValue='Hide other uses of the word Golf';
    
    			  } else {
    				  target.style.display = "none";
                      //change text
                      aRef.firstChild.nodeValue='Show other uses of the word Golf';
    			  }
      }
    
    
    }
    and change
    <a href="#" class="more" onclick="toggle('sub_section') ;return false;">Other uses of the word Golf</a>

    with

    <a href="#" class="more" onclick="toggle(this,'sub_section') ;return false;">Hide other uses of the word Golf</a>

  3. #3
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick question. THe word "Golf" is going to be dynamic, so can we make this so the word "Golf" stays in the HTML link, and the javascript writes only the worlds


    "Hide other uses of the word"

    "Show other uses of the word"

    ?

    Thanks

  4. #4
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could send the word as a parameter to the toggle function.

  5. #5
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    er...sorry, I have no clue what that means I am a much more visual person when it comes to program logic.

    So in yoru example, where would I send the word "Golf" as a parameter?

  6. #6
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here is the code:
    Code:
    function toggle(aRef,targetId,word){
      if (document.getElementById){
    
    		  target = document.getElementById( targetId );
    			  if (target.style.display == "none"){
    				  target.style.display = "";
                      //change text
                       aRef.firstChild.nodeValue='Hide other uses of the word '+word;
    
    			  } else {
    				  target.style.display = "none";
                      //change text
                      aRef.firstChild.nodeValue='Show other uses of the word '+word;
    			  }
    
      }
    }
    and 'a' tag:
    <a href="#" class="more" onclick="toggle(this,'sub_section','Golf') ;return false;">Hide other uses of the word Golf</a>

  7. #7
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet. Thanks. Let me run it

  8. #8
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like a glove !

    That rocks, thanks.


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
  •