Jump to content

Recommended Posts

I'm using this javascript that show/hides multiple divs.  It works but I want my text to change when I click on the link.  by default it says Show after clicking on it I want it to change to Hide.  Here is my code so far.

 

JavaScript

// JavaScript Document
var hidden = true;
function getElementsByClass(searchClass, domNode, tagName) {
if (domNode == null) domNode = document;
if (tagName == null) tagName = '*';
var el = new Array();
var tags = domNode.getElementsByTagName(tagName);
var tcl = " "+searchClass+" ";
for(i=0,j=0; i<tags.length; i++) {
	var test = " " + tags[i].className + " ";
	if (test.indexOf(tcl) != -1)
	el[j++] = tags[i];
}
return el;
}

function toggle_hideme()
{

hidden = !hidden;
var newDisplay;
if(hidden)
{
	newDisplay = 'none';

}
else
{
	newDisplay = 'block';

}
var hellos = getElementsByClass("div_class_name", null, "div");
for(var i = 0; i < hellos.length; i++)
{
	hellos[i].style.display = newDisplay;
}
}

 

HTML

<a href="#" onClick="toggle_hideme('div_class_name');">Show</a>
<div class="div_class_name" style="display:none"> 
Blah Blah Blah
</div>
<div class="something_else">
What ever is here will not disappear!
</div>
<div class="div_class_name" style="display:none">
more blah blah blah
</div>

Link to comment
https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/
Share on other sites

<a href="#" onClick="toggle_hideme('div_class_name');this.innerHTML='hide'">Show</a>

 

you can also use a function if you want the code to be external.

 

But will it go back to show if they click it again.  Like it starts out as Show, you click it, it changes to Hide, you click it again and it goes back to Show

Okay, I see what your want to accomplish.

 

<script type="text/javascript">
function toggleText(obj) 
{
if(obj.innerHTML == "Show") {
   obj.innerHTML = "Hide";
}else if(obj.innerHTML == "Hide") {
   obj.innerHTML = "Show";
}
}
</script>

<a href="#" onClick="toggle_hideme('div_class_name');toggleText(this);">Show</a>

Okay, I see what your want to accomplish.

 

<script type="text/javascript">
function toggleText(obj) 
{
if(obj.innerHTML == "Show") {
   obj.innerHTML = "Hide";
}else if(obj.innerHTML == "Hide") {
   obj.innerHTML = "Show";
}
}
</script>

<a href="#" onClick="toggle_hideme('div_class_name');toggleText(this);">Show</a>

 

Perfect. Thanks

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.