/    Sign up×
Community /Pin to ProfileBookmark

Toggle problem

In the following code, all displays toggle with clicking on the up/down arrows
EXCEPT the first one. It does not toggle correctly. It closes the opened text, but does not open it when closed.

I’ve been staring at this too long. Can anyone spot my mistake?
I think it is in the ‘toggleAll’ function.

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Toggle List </title>

<style type=”text/css”>
#ulDiv { width:500px; }
ul { color: blue; list-style-type: none; }
li p { padding-left: 2em; display:none; }

.arrow { color:blue; background-Color:lightgreen; cursor:pointer; font-size:1.5em; }
</style>

</head>
<body>
<div id=”ulDiv”>
<ul>

<li><span class=”arrow” id=”toggleAllBtn”>&#x25BC;</span> Toggle All <p></p></li>

<li><span class=”arrow”>&#x25BC;</span> Test 1
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent euismod ultrices ante, ac laoreet nulla vestibulum adipiscing. Nam quis justo in augue auctor imperdiet. Curabitur aliquet orci sit amet est posuere consectetur. Fusce nec leo ut massa viverra venenatis. Nam accumsan libero a elit aliquet quis ullamcorper arcu tincidunt. Praesent purus turpis, consectetur quis congue vel, pulvinar at lorem. Vivamus varius condimentum dolor, quis ultricies ipsum porta quis.
</p></li>

<li><span class=”arrow”>&#x25BC;</span> Test 2
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent euismod ultrices ante, ac laoreet nulla vestibulum adipiscing. Nam quis justo in augue auctor imperdiet. Curabitur aliquet orci sit amet est posuere consectetur. Fusce nec leo ut massa viverra venenatis. Nam accumsan libero a elit aliquet quis ullamcorper arcu tincidunt.
</p></li>

<li><span class=”arrow”>&#x25BC;</span> Test 3
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent euismod ultrices ante, ac laoreet nulla vestibulum adipiscing. Nam quis justo in augue auctor imperdiet. Curabitur aliquet orci sit amet est posuere consectetur.
</p></li>

</ul>
</div>

<script type=”text/javascript”>
// Idea from: http://stackoverflow.com/questions/868407/hide-an-elements-next-sibling-with-javascript

window.onload = function() {
var sel = document.getElementsByClassName(‘arrow’); var x;
for (var i=0; i<sel.length; i++) {
sel[i].value = ‘&#x25BC;’;
sel[i].onclick = function() { toggleArrow(this); }
x = sel[i].nextElementSibling; x.style.display = ‘none’;
}
sel[0].onclick = function() { toggleAll(); }
}

function toggleArrow(info) {
info.value = (info.value == ‘&#x25BC;’) ? ‘&#x25B2;’ : ‘&#x25BC;’; info.innerHTML = info.value;
var x = info.nextElementSibling;
if (x.style.display == ‘none’) { x.style.display = ‘block’; } else { x.style.display = ‘none’; }
}
/* */
function toggleAll() {
var sel = document.getElementsByClassName(‘arrow’);
var x = sel[0].value;
if (x == ‘&#x25BC;’) { x = ‘&#x25BC;’; } else { x = ‘&#x25B2;’; }

var y;
for (var i=0; i<sel.length; i++) {
sel[i].value = x; // alert(x);
sel[i].innerHTML = x;
y = sel[i].nextElementSibling;
if (x == ‘&#x25BC;’) { y.style.display = ‘none’; } else { y.style.display = ‘block’; }
}
}
/* */
</script>

</body>
</html>

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyFeb 26.2015 — Looks like the problem is a simple logic slip-up. In your [B]toggleAll()[/B] function where you set the value of [B]x[/B] based on your [I]if/else[/I], if the value is [B]&#x25BC;[/B] you set the value to [B]&#x25BC;[/B], thus seeing no result. Just reverse the condition or the values and you should be good to go.
Copy linkTweet thisAlerts:
@JMRKERauthorFeb 26.2015 — Thanks.

Staring too long at a problem leads to goofy decisions. (Programming rule #5 ? )
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...