Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hide/show div

Status
Not open for further replies.

superfly404

Technical User
Feb 1, 2005
24
US
I have a hide show div that I need to bold the link when it's clicked to show and unbold when the link hides, any ideas what I need to change

<script>
function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
</script>

<a href="#" onclick="showhide('1'); return(false);">Text Link</a></li>
<div style="display: none;" id="1">
 
any ideas what I need to change
Yeah, first thing's first - indent your code!!
I don't know how you guys/girls passed CS101 w/o learning to indent. I know my professors marked off big points for not indenting. It makes code 9999999999999 times easier to read and debug. Now, for your real question:

I've highlighted the things to change in red. Additionally, I made the function return false, and just returned the value from the function in the link, that avoids having to run 2 javascript statements in the onclick handler of the link - I just think it looks cleaner, either way works fine.

Code:
<script>
function showhide(id, [COLOR=red][b]linkObj[/b][/color]) {
   if (document.getElementById) {
      obj = document.getElementById(id);
      if (obj.style.display == "none") {
         obj.style.display = "";
         [COLOR=red][b]linkObj.style.fontWeight = 'bold';[/b][/color]
      } else {
         obj.style.display = "none";
         [COLOR=red][b]linkObj.style.fontWeight = 'normal';[/b][/color]
      }
   }
   return false;
}
</script>

<a href="#" onclick="[COLOR=red][b]return[/b][/color] showhide('1', [COLOR=red][b]this[/b][/color])">Text Link</a></li>
<div style="display: none;" id="1">This is the div that we're hiding</div>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
thanks, appreciate the help! what would I do if I wanted to do an exand all and a close all link?

thanks
 
I'd suggest writing a loop to call your function for each div. Pass the id of the div and a reference to the link for each function call.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top