lilgezuz Posted January 23, 2012 Share Posted January 23, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/ Share on other sites More sharing options...
AyKay47 Posted January 23, 2012 Share Posted January 23, 2012 <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. Quote Link to comment https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/#findComment-1310196 Share on other sites More sharing options...
lilgezuz Posted January 23, 2012 Author Share Posted January 23, 2012 <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 Quote Link to comment https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/#findComment-1310200 Share on other sites More sharing options...
AyKay47 Posted January 23, 2012 Share Posted January 23, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/#findComment-1310202 Share on other sites More sharing options...
lilgezuz Posted January 23, 2012 Author Share Posted January 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255558-need-help-with-onclick/#findComment-1310213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.