/    Sign up×
Community /Pin to ProfileBookmark

Adding Arrows to a Div Toggle – Help Needed

Hi everyone,

I was wondering if someone could help me with some javascript for a site I am creating. I am creating a div toggle type menu, in which links hide and show div boxes to change the content.

I am using some code I found online and was wondering if someone could assist me in making one change to it. I am looking to dynamically hide arrows in addition to the content divs. Here is the code I am using at the moment:

javascript:

[CODE]<script language=”javascript”>
addEvent(window, ‘load’, et_init);

var et_toggleElements = [];

/* Initialisation */
function et_init() {
var i, link, id, target, first;
first = true;
for (i = 0; (link = document.links[i]); i++) {
if (/btoggleb/.exec(link.className)) {
id = link.href.split(‘#’)[1];
target = document.getElementById(id);
et_toggleElements[et_toggleElements.length] = target;
if (first) {
first = false;
} else {
target.style.display = ‘none’;
}
link.onclick = et_toggle;
}
}
}

function et_toggle(e) {

/* Gets the URL */
if (typeof e == ‘undefined’) {
var e = window.event;
}
var source;
if (typeof e.target != ‘undefined’) {
source = e.target;
} else if (typeof e.srcElement != ‘undefined’) {
source = e.srcElement;
} else {
return true;
}

/* Toggle Boxes */
if (source.nodeType == 3) {
source = source.parentNode;
}
var id = source.href.split(‘#’)[1];
var elem;
for (var i = 0; (elem = et_toggleElements[i]); i++) {
if (elem.id != id) {
elem.style.display = ‘none’;
} else {
elem.style.display = ‘block’;
}
}
return false;
}

function addEvent(obj, evType, fn){
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent(“on”+evType, fn);
return r;
} else {
return false;
}
}
</script>[/CODE]

Html:

[code=html]<table border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr><td><a href=”#one” class=”toggle”>Navigating the Course</a></td><td><img align=”right” src=”arrow.jpg”/></td></tr>
<tr><td><a href=”#two” class=”toggle”>Course Requirements</a></td><td><img align=”right” src=”arrow.jpg”/></td></tr>
<tr><td><a href=”#three” class=”toggle”>FAQ’s</a></td><td><img align=”right” src=”arrow.jpg”/></td></tr>
<tr><td><a href=”#four” class=”toggle”>Fine Print</a></td><td><img align=”right” src=”arrow.jpg”/></td></tr>
<tr><td><a href=”#five” class=”toggle”>Help</a></td><td><img align=”right” src=”arrow.jpg”/></td></tr>
</table>[/code]

From what I understand of the javascript, it seems to be dynamically setting the id’s of the links based on if they are set to the toggle class. Then using that it determines which one matches the current item clicked and displays that content box while hiding the rest.

My issue is how can I make the images do the same thing. Do i have to do the same thing done to the links ( aka setting them to a class and setting there id ) or is there an easier way to do this?

Anyhelp will be greatly appreciated, thanks

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsOct 15.2008 — [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" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
addEvent(window, 'load', et_init);

var et_toggleElements = [];
var et_toggleimg = [];

/* Initialisation */
function et_init() {
var i, link, id, target, first;
first = true;
for (i = 0; (link = document.links[i]); i++) {
if (/btoggleb/.exec(link.className)) {
id = link.href.split('#')[1];
target = document.getElementById(id);
var tr=link;
while (tr.parentNode){
if (tr.nodeName=='TR') break;
tr=tr.parentNode;
}
et_toggleimg.push(tr.getElementsByTagName('IMG')[0]);
et_toggleElements[et_toggleElements.length] = target;
if (first) {
if (et_toggleimg[i]) et_toggleimg[i].src=et_toggleimg[i].src.replace('down','up');
first = false;
}
else {
target.style.display = 'none';
}
link.onclick = et_toggle;
}
}
}

function et_toggle(e) {

/* Gets the URL */
if (typeof e == 'undefined') {
var e = window.event;
}
var source;
if (typeof e.target != 'undefined') {
source = e.target;
} else if (typeof e.srcElement != 'undefined') {
source = e.srcElement;
} else {
return true;
}


/* Toggle Boxes */
if (source.nodeType == 3) {
source = source.parentNode;
}
var id = source.href.split('#')[1];
var elem;
var imgs=et_toggleimg
for (var i = 0; (elem = et_toggleElements[i]); i++) {
if (elem.id != id) {
elem.style.display = 'none';
if (imgs[i]) imgs[i].src=imgs[i].src.replace('up','down');
}
else {
if (imgs[i]) imgs[i].src=imgs[i].src.replace('down','up');
elem.style.display = 'block';
}
}
return false;
}

function addEvent(obj, evType, fn){
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}
/*]]>*/
</script></head>

<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<a href="#one" class="toggle">Navigating the Course</a></td>
<td>
<img align="right" src="http://www.vicsjavascripts.org.uk/StdImages/down[1].gif"/></td>
</tr>
<tr><td><a href="#two" class="toggle">Course Requirements</a></td><td><img align="right" src="http://www.vicsjavascripts.org.uk/StdImages/down[1].gif"/></td></tr>
<tr><td><a href="#three" class="toggle">FAQ's</a></td><td><img align="right" src="http://www.vicsjavascripts.org.uk/StdImages/down[1].gif"/></td></tr>
<tr><td><a href="#four" class="toggle">Fine Print</a></td><td><img align="right" src="http://www.vicsjavascripts.org.uk/StdImages/down[1].gif"/></td></tr>
<tr><td><a href="#five" class="toggle">Help</a></td><td><img align="right" src="http://www.vicsjavascripts.org.uk/StdImages/down[1].gif"/></td></tr>
</table>

<div id="one" style="width:100px;height:20px;" >1</div>
<div id="two" style="width:100px;height:20px;" >2</div>
<div id="three" style="width:100px;height:20px;" >3</div>
<div id="four" style="width:100px;height:20px;" >4</div>
<div id="five" style="width:100px;height:20px;" >5</div>
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@PaulMatosauthorOct 15.2008 — thxs a ton ?
Copy linkTweet thisAlerts:
@PaulMatosauthorOct 20.2008 — Sorry, but I just have one more issue. This code seems to breakdown in IE. I was wondering If you could help me with which part of the code IE can't process and how I may fix it. Here is the URL:


http://orea.courseplex.com/WP/

and the code I'm working with:

[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
addEvent(window, 'load', et_init);

var et_toggleElements = [];
var et_toggleimg = [];
var ImageArray = [];
ImageArray[0] = 'navigate.gif';
ImageArray[1] = 'course-requirements.gif';
ImageArray[2] = 'faqs.gif';
ImageArray[3] = 'fine-print.gif';
ImageArray[4] = 'help.gif';

/* Initialisation */
function et_init() {
var i, link, id, target, first;
first = true;
for (i = 0; (link = document.links[i]); i++) {
if (/btoggleb/.exec(link.className)) {
id = link.href.split('#')[1];
target = document.getElementById(id);
var tr=link;
while (tr.parentNode){
if (tr.nodeName=='TR') break;
tr=tr.parentNode;
}
et_toggleimg.push(tr.getElementsByTagName('IMG')[0]);
et_toggleElements[et_toggleElements.length] = target;
if (first) {
if (et_toggleimg[i]) et_toggleimg[i].src=et_toggleimg[i].src.replace('down','up');
first = false;
}
else {
target.style.display = 'none';
}
link.onclick = et_toggle;
}
}
}

function et_toggle(e) {




/* Toggle Boxes */
if (source.nodeType == 3) {
source = source.parentNode;
}
var id = source.href.split('#')[1];


var elem;
var imgs=et_toggleimg
for (var i = 0; (elem = et_toggleElements[i]); i++) {
if (elem.id != id) {
elem.style.display = 'none';
if (imgs[i]) imgs[i].src=imgs[i].src.replace('up','down');
}
else {
if (imgs[i]) imgs[i].src=imgs[i].src.replace('down','up');
elem.style.display = 'block';
document.PageImage.src = ImageArray[i];

}
}
return false;
}

function addEvent(obj, evType, fn){
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}


function toggleNext2(el,tname,first) {
var next=el.nextSibling;
var tags=el.parentNode.getElementsByTagName(tname);
while(next.nodeType != 1) next = next.nextSibling;
next.style.display=((next.style.display=="none") ? "block" : "none");
if (first!=1){
for (i=0; i<tags.length; i++) {
var tohide=tags[i].nextSibling;
while(tohide.nodeType != 1) tohide = tohide.nextSibling;
if (tohide!=next){tohide.style.display="none";}
}
}
}

function toggleNextByIdAndTag2(el,tname) {
var ccn="clicker";
clickers=document.getElementById(el).getElementsByTagName(tname);
for (i=0; i<clickers.length; i++) {
clickers[i].className+=" "+ccn;
clickers[i].onclick=function() {toggleNext2(this,tname)}
toggleNext2(clickers[i],tname,1);
}
}

window.onload=function(){toggleNextByIdAndTag2('dlist','dt')}

/*]]>*/
</script>


<style media="screen" type="text/css">

body { font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000000; }

dt { color:#0000FF; cursor:pointer;}

.Container { }

.Header { padding-left:20px; padding-top:20px; background-color:#999999; height:90px; color:#FFFFFF;}

.Content { float:left; margin-left:60px; margin-top:20px; width:75%; padding-bottom:10px; z-index:2;}

.HBar { background-color:#333333; height:2px; width:100%; margin-top:10px;}

.VBar { float:left; width:2px; background-color:#333333;}

.Menu { float:left; padding:20px;}

.BottomBar { background-color:#999999; height:20px; width:100%; margin-top:10px; float:left;}

#PageImage { margin:25px;}

</style>

</head>

<body>

<div class="Container">
<div class="Header">
<h1>
Welcome to the OREA Real Estate College Online Course
</h1>
</div>

<div class="HBar">

</div>

<div class="Menu">
<table border="0" cellpadding="7" cellspacing="0"><tr>
<td><a href="#one" class="toggle">Navigating the Course</a></td><td><img alt="" align="right" src="down[1].gif"></img></td>
</tr><tr>
<td><a href="#two" class="toggle">Course Requirements</a></td><td><img alt="" align="right" src="down[1].gif"></img></td>
</tr><tr>
<td><a href="#three" class="toggle">FAQ's</a></td><td><img alt="" align="right" src="down[1].gif"></img></td>
</tr><tr>
<td><a href="#four" class="toggle">Fine Print</a></td><td><img alt="" align="right" src="down[1].gif"></img></td>
</tr><tr>
<td><a href="#five" class="toggle">Help</a></td><td><img alt="" align="right" src="down[1].gif"></img></td>
</tr><tr>
<td><img id="PageImage" src="navigate.gif" alt=""></img></td>
</tr></table>
</div>

<div class="VBar" style="height:400px;">
</div>

<div id="one" style="display:block" class="Content">
<p>To take the course in sequence:<br /><br></br><br></br>
1) <u>Click</u> the <b><Previous or Next></b> arrows from the top right of the screen.<br></br><br></br>
<br></br><br></br><br></br>
To access any Session of the course at any time:<br></br><br></br>
1)<u>Click</u> on the <b>Drop Down Arrow</b> from <b>Jump to...</b> at the top right of screen<br></br><br></br>
2)<u>Select</u> on the topic to be taken to that section of the course<br></br>
<br></br><br></br><br></br>
Smart Bookmarking:<br></br>
The course will automatically return to the last page visited.<br></br>
</p>
</div>

<div id="two" style="display:none" class="Content">
<p>To complete the course, the following must be done in this order.<br /><br />
1) <u>Complete</u> each <b>Session</b> of the course.<br></br>
2) <u>Pass</u> each <b>Session Exercise(Quiz).</b><br></br>
3) <u>Complete</u> the final <b>Assignment(Quiz).</b><br></br>
4) <u>Complete</u> the <b>Course Survey and Declaration.</b><br></br>
5) <u>Go</u> to <b>Course History</b> in <b>My Portfolio</b> to view MCE Credits or retieve transcript.<br></br>
<br></br><br></br>
* No paper certificate will be provided
</p>
</div>

<div id="three" style="display:none" class="Content">
<dl id="dlist">
<dt>Q: How do I get back to where I left off when I return to the course?</dt>
<dd><table><tr><td><img src="dots.jpg"/></td><td>A: The system uses a smart bookmarking system to remember the last page <br />you were on before logging out</td></tr></table></dd>
<dt>Q: How many sessions are there in the course that I am taking?</dt>
<dd><table><tr><td><img src="dots.jpg"/></td><td>A: The sessions are numbered on the left hand navigation pane. This is a quick method <br />to know how many sessions are in the course. Keep
in mind that there may be many different topics within each session.</td></tr></table></dd>
<dt>Q: Why can I not access the Final Assignment(Quiz)?</dt>
<dd><table><tr><td><img src="dots.jpg"/></td><td>A: All Session quizes must be completed before attempting the final assignment (quiz).<br /> Click on Exercise List on the top banner of the course to view the status <br />of each session quiz.</td></tr></table></dd>
<dt>Q: What do I do after I have completed the course?</dt>
<dd><table><tr><td><img src="dots.jpg"/></td><td>A: Go to My Portfolio and check under Course History -> Completed Courses <br />to view your MCE Credits.</td></tr></table></dd>
</dl>
</div>

...

[/CODE]


It was too long :S


But as far as I can tell, The arrows seem to work if you click the links from top to bottom, but once you go up they duplicate. The content in the middle also seems to be duplicating. And lastly the images for each page are not changing.
Copy linkTweet thisAlerts:
@vwphillipsOct 20.2008 — [CODE]function et_toggle(e) {

/* Gets the URL */
if (typeof e == 'undefined') {
var e = window.event;
}
var source;
if (typeof e.target != 'undefined') {
source = e.target;
} else if (typeof e.srcElement != 'undefined') {
source = e.srcElement;
} else {
return true;
}



/* Toggle Boxes */
if (source.nodeType == 3) {
source = source.parentNode;
}
var id = source.href.split('#')[1];


var elem;
var imgs=et_toggleimg
for (var i = 0; (elem = et_toggleElements[i]); i++) {
if (elem.id != id) {
elem.style.display = 'none';
if (imgs[i]) imgs[i].src=imgs[i].src.replace('up','down');
}
else {
if (imgs[i]) imgs[i].src=imgs[i].src.replace('down','up');
elem.style.display = 'block';
[COLOR="Red"]document.getElementById('PageImage').src[/COLOR] = ImageArray[i];

}
}
return false;
}
[/CODE]


tested with IE and Moz, the arrows seem fine
Copy linkTweet thisAlerts:
@PaulMatosauthorOct 20.2008 — Wow thanks a ton man, however I am getting duplication on the FAQ and Fine print page, any idea on what is causing that?
Copy linkTweet thisAlerts:
@PaulMatosauthorOct 20.2008 — turned out it was part of the css. Thanks again for all your help.
×

Success!

Help @PaulMatos 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.16,
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,
)...