Jump to content

Need help with onclick


lilgezuz

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.