/    Sign up×
Community /Pin to ProfileBookmark

JavaScript Advice

Hi All,

I was trying to write a script that would hide/show different div’s on a webpage with the click of a button. I have a “hide all” button, a “show all” button, and buttons to minimize three different div elements, which are categorized by three separate css classes (observation, interpretation, application). I managed to get the script working properly, but I was looking for some advice on how to better write this code (it all seems redundant).

I know there has to be a better way to use less code, and have each button reference the same function(s). (Currently I have a function set up for each button.) Also, I was wondering if there was a way to have the “hide all” button show on the screen, but when you press that button, it gets replaced by the “show all” button. Any advice and pointers would be great because as I am learning JavaScript, I would like to learn it the correct way (I don’t want learn bad coding practices) Thanks in advance. The code is posted below:

[CODE]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-type” content=”text/html;charset=UTF-8″ />

<title>Bible Study Notes</title>
<script type=”text/javascript”>
/* <![CDATA[ */
function hideAll() {
var hideAll = document.getElementsByTagName(‘div’);
for(var i=0; i<hideAll.length; ++i) {
if(hideAll[i].className == ‘observation’) {
hideAll[i].style.display = ‘none’;
}
if(hideAll[i].className == ‘interpretation’) {
hideAll[i].style.display = ‘none’;
}
if(hideAll[i].className == ‘application’) {
hideAll[i].style.display = ‘none’;
}
}
document.onclick = hideAll;
return false;
}
function showAll() {
var showAll = document.getElementsByTagName(‘div’);
for(var j=0; j<showAll.length; ++j) {
if(showAll[j].className == ‘observation’) {
showAll[j].style.display = ‘block’;
}
if(showAll[j].className == ‘interpretation’) {
showAll[j].style.display = ‘block’;
}
if(showAll[j].className == ‘application’) {
showAll[j].style.display = ‘block’;
}
}
document.onclick = showAll;
return false;
}
function hideOb() {
var hideOb = document.getElementsByTagName(‘div’);
for(var i=0; i<hideOb.length; ++i) {
if(hideOb[i].className == ‘observation’) {
hideOb[i].style.display = ‘none’;
}
}
document.onclick = hideOb;
return false;
}
function hideIn() {
var hideIn = document.getElementsByTagName(‘div’);
for(var i=0; i<hideIn.length; ++i) {
if(hideIn[i].className == ‘interpretation’) {
hideIn[i].style.display = ‘none’;
}
}
document.onclick = hideIn;
return false;
}
function hideAp() {
var hideAp = document.getElementsByTagName(‘div’);
for(var i=0; i<hideAp.length; ++i) {
if(hideAp[i].className == ‘application’) {
hideAp[i].style.display = ‘none’;
}
}
document.onclick = hideAp;
return false;
}

/* ]]> */
</script>
<style type=”text/css”>
#header {
text-align: center;
}
#maincontent {
line-height: 1.5em;
font-size: 110%;
}
.verse {
font-style: italic;
color: 000000;
}
.observation, .interpretation, .application {
color: #000FF0;
}
.max {
display: inline;
visibility: hidden;
}
input.b {
width: 30px;
height: 25px;
}
</style>
</head>

<body>
<div id=”header”>
<h1>The Book of Exodus Chapter 1</h1>
</div>
<div id=”maincontent”>
<div id=”icons”>
<form name=”toggleControl” action=”” method=”post”>
<input type=”button” value=” hide ” onclick=”hideAll()” title=”Hide all fields” />&nbsp;&nbsp;<input type=”button” value=” show ” onclick=”showAll()” title=”Expand all fields”/>&nbsp;&nbsp;<input type=”button” class=”b” value=” o ” onclick=”hideOb()” title=”Hide Observation” />&nbsp;&nbsp;<input type=”button” class=”b” value=” i ” onclick=”hideIn()” title=”Hide Interpretation” />&nbsp;&nbsp;<input type=”button” class=”b” value=” a ” onclick=”hideAp()” title=”Hide Application” />
</form>
</div>
<div class=”verse”>
This is the first verse of Exodus Chapter 1
</div>
<div class=”observation”>
Observations of chapter 1 verse 1
</div>
<div class=”interpretation”>
Interpretation of chapter 1 verse 1
</div>
<div class=”application”>
Application from chapter 1 verse 1
</div>
<div class=”verse”>
This is the second verse of Exodus Chapter 1
</div>
<div class=”observation”>
Observations of chapter 1 verse 2
</div>
<div class=”interpretation”>
Interpretation of chapter 1 verse 2
</div>
<div class=”application”>
Application from chapter 1 verse 2
</div>
</div>
</body>
</html>
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 20.2011 — I could not tell from your example if you wanted the so, si and sa buttons to go

with each verse or as I have coded it below as all verses.
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-type" content="text/html;charset=UTF-8" /&gt;
&lt;title&gt;Bible Study Notes&lt;/title&gt;

&lt;style type="text/css"&gt;
#header { text-align: center; }
#maincontent {
line-height: 1.5em;
font-size: 110%;
}
.verse {
font-style: italic;
color: 000000;
}
.observation, .interpretation, .application { color: #000FF0; }
.max {
display: inline;
visibility: hidden;
}
input.b {
width: 30px;
height: 25px;
}
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=247837

/* &lt;![CDATA[ */
function flipAll(elem) {
var sel = document.getElementsByTagName('div');
switch (elem) {
case 'hide' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'observation') { sel[i].style.display = 'none'; }
document.getElementById('shO'). value = 'so';
if (sel[i].className == 'interpretation') { sel[i].style.display = 'none'; }
document.getElementById('shI'). value = 'si';
if (sel[i].className == 'application') { sel[i].style.display = 'none'; }
document.getElementById('shA'). value = 'sa';
} break;
case 'show' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'observation') { sel[i].style.display = 'block'; }
document.getElementById('shO'). value = 'ho';
if (sel[i].className == 'interpretation') { sel[i].style.display = 'block'; }
document.getElementById('shI'). value = 'hi';
if (sel[i].className == 'application') { sel[i].style.display = 'block'; }
document.getElementById('shA'). value = 'ha';
} break;
}
// document.onclick = flipAll;
return false;
}

function flip(elem) { // alert('className: '+elem.className+'nvalue: '+elem.value);
var sel = document.getElementsByTagName('div');
switch (elem.value) {
case 'so' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'observation') { sel[i].style.display = 'block'; } elem.value='ho';
} break;
case 'si' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'interpretation') { sel[i].style.display = 'block'; } elem.value='hi';
} break;
case 'sa' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'application') { sel[i].style.display = 'block'; } elem.value='ha';
} break;
case 'ho' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'observation') { sel[i].style.display = 'none'; } elem.value='so';
} break;
case 'hi' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'interpretation') { sel[i].style.display = 'none'; } elem.value='si';
} break;
case 'ha' : for (var i=0; i&lt;sel.length; ++i) {
if (sel[i].className == 'application') { sel[i].style.display = 'none'; } elem.value='sa';
} break;
}
// document.onclick = flip;
return false;
}

onload = function() {
flipAll('hide');
}

/* ]]&gt; */
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="header"&gt;
&lt;h1&gt;The Book of Exodus Chapter 1&lt;/h1&gt;
&lt;/div&gt;

&lt;div id="maincontent"&gt;
&lt;div id="icons"&gt;
&lt;form name="toggleControl" action="" method="post" onsubmit="return false"&gt;
All
&lt;input type="button" value="show" onclick="flipAll('show')" title="Expand all fields"/&gt;&amp;nbsp;&amp;nbsp;
&lt;input type="button" value="hide" onclick="flipAll('hide')" title="Collapse all fields"/&gt;&amp;nbsp;&amp;nbsp;

Toggle
&lt;input id="shO" type="button" class="b" value="so" onclick="flip(this)" title="Show/Hide Observation" /&gt;&amp;nbsp;&amp;nbsp;
&lt;input id="shI" type="button" class="b" value="si" onclick="flip(this)" title="Show/Hide Interpretation" /&gt;&amp;nbsp;&amp;nbsp;
&lt;input id="shA" type="button" class="b" value="sa" onclick="flip(this)" title="Show/Hide Application" /&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;p&gt;

&lt;div class="verse"&gt;
This is the first verse of Exodus Chapter 1
&lt;/div&gt;

&lt;div class="observation"&gt;
Observations of chapter 1 verse 1
&lt;/div&gt;

&lt;div class="interpretation"&gt;
Interpretation of chapter 1 verse 1
&lt;/div&gt;

&lt;div class="application"&gt;
Application from chapter 1 verse 1
&lt;/div&gt;

&lt;div class="verse"&gt;
This is the second verse of Exodus Chapter 1
&lt;/div&gt;

&lt;div class="observation"&gt;
Observations of chapter 1 verse 2
&lt;/div&gt;

&lt;div class="interpretation"&gt;
Interpretation of chapter 1 verse 2
&lt;/div&gt;

&lt;div class="application"&gt;
Application from chapter 1 verse 2
&lt;/div&gt;

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

Let me know if this does not make sense to you.
Copy linkTweet thisAlerts:
@jrg_newtojsauthorJun 20.2011 — Wow...I was trying to figure out how to use a switch statement in this situation, but I couldn't figure it out. Looking at the code you posted makes a lot of sense now. What you did with the Toggle buttons was going to be my next project. Thanks for that.

Although I do not understand a few things:

1) I don't understand why there is a .value in the second switch statement. What does that do in this situation?

2) In the <input> of the form, the onclick event says "flip(this)". How does that work?

what is the "this" part doing/referencing?


3) In the <form> section, you placed an onsubmit="return false", I just don't understand why.

Thanks for tolerating the newbie questions, I just don't completely understand yet.
Copy linkTweet thisAlerts:
@AJAXnubJun 20.2011 — 1) value is a property that all input objects have. Its basically what they send to the server when submitted, and in some types it reflects visually. here it was used just to store information.

2)'this' was referring to the object that triggered the event, so this.value=the value of the button (in the script it won't be like that, because it is outside the button's event).

3)What that does is basically trigger when the form attempts to submit, and stops it. This prevents the page from refreshing and resetting any data that was changed.
Copy linkTweet thisAlerts:
@JMRKERJun 20.2011 — 1) value is a property that all input objects have. Its basically what they send to the server when submitted, and in some types it reflects visually. here it was used just to store information.

2)'this' was referring to the object that triggered the event, so this.value=the value of the button (in the script it won't be like that, because it is outside the button's event).

3)What that does is basically trigger when the form attempts to submit, and stops it. This prevents the page from refreshing and resetting any data that was changed.[/QUOTE]


Thanks for explaining it to him/her.

Saved me a bunch of words. ?
×

Success!

Help @jrg_newtojs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...