Jump to content

[SOLVED] Changing a class depending on what user clicks


dc_jt

Recommended Posts

Ive got the following which when 'The Resort' is clicked the links below either appear or hide. This works fine, however I would like the class to also change to selected when the links are shown.

 

<script type="text/javascript">
function showHide(link) {
var theDiv = link.parentNode.getElementsByTagName("div")[0];
if (link.innerHTML == "The Resort") {
	// show the div
	theDiv.style.display = "";
	link.innerHTML = "The Resort ";
} else {
	// hide the div
	theDiv.style.display = "none";

	link.innerHTML = "The Resort";
}
}
</script>
</head>
<body>

<div id="sidelinks">
<div class="additionalInfo">

<ul><li><a href="nojavascript.html" onclick="showHide(this);return false;">The Resort</a>

<div style="display:none;">
<li class="sub"><a href="#" class="sub_link">-   The Hotel</a></li>
<li class="sub"><a href="#" class="sub_link">-   Packages & Tarrifs</a></li>
<li class="sub"><a href="#" class="sub_link">-   Guest Book</a></li>
</div>
</div>

 

The above shows it with no class but I want it to be like this:

 

<ul><li><a href="nojavascript.html" class="selected" onclick="showHide(this);return false;">The Resort</a>

 

When the links are shown.

 

How can I do this?

 

Thanks

Link to comment
Share on other sites

Thanks alot goose, the link.className one worked perfectly :)

 

Now that was just for the 'The Resort' one, I have about five others that I want to do in the same way.

 

Do I have to do write out javascript for each one or is there a way I can use the same one?

 

Thanks

Link to comment
Share on other sites

Sorry probably my fault.

 

For example, I have a menu on the left of my page with 3 headings, say heading 1, heading 2, heading 3.

 

When I click heading 1, heading sub1.1 and heading sub1.2 appear.

 

This all works fine with the script above.

 

However, I now want to do the same for heading 2 and heading 3.

 

Can I use the same showHide function or do I need to create individual ones for each heading?

 

Hope that makes more sense?

 

Thanks

Link to comment
Share on other sites

Yes that makes very much sense! This is how I would do that.

 

HTML Part

<a href="nojavascript.html"" id="resort" onclick="showHide(this); return false;">The Resort</a>

<div id="resort_sub" style="display: none;">
...
</div>

 

JS Part

function showHide(link) {
var theDiv = document.getElementById(link.id + '_sub');
if(theDiv.style.display == 'block'){
	theDiv.style.display = 'none';
	link.className = 'something';
} else {
	theDiv.style.display = 'block';
	link.className = 'something_else';
}
}

 

What I did here was make a more generic function that you could use for multiple cases. It does the checking based on the id of the link clicked. So they way I would name a link would be id="resort" or something else and have the sub links for each main link be in a div called id="resort_sub". Let me know if this doesn't make sense.

 

<html>
<head>
	<script type="text/javascript">
		function showHide(link) {
			var theDiv = document.getElementById(link.id + '_sub');
			if(theDiv.style.display == 'block'){
				theDiv.style.display = 'none';
				link.className = 'something';
			} else {
				theDiv.style.display = 'block';
				link.className = 'something_else';
			}
		}
	</script>
</head>
<body>
	<a href="nojavascript.html"" id="resort" onclick="showHide(this); return false;">The Resort</a>

	<div id="resort_sub" style="display: none;">
		...
	</div>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.