/    Sign up×
Community /Pin to ProfileBookmark

Javascript loop terminates by some reason

When I do only an if statement the iteration loops as expected:

“`
if (list[i].id === page) {
list[i].id.style.display = “block”;
}

“`

https://jsfiddle.net/q0yxd1sc/2/

But if I add an else statement it stops after the first item:

“`
if (list[i].id === page) {
list[i].id.style.display = “block”;
} else{
list[i].id.style.display = “none”;
}
“`

https://jsfiddle.net/jge5q3za/3/

What am I doing wrong?
Besides this no elements are hidden. Any tip?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 22.2019 — The console of your browser helps you a lot locating such errors. The error is located in this line:

`list[i].id.style.display = "block";</C><br/>
<C>
id</C> is obsolete.<br/>
Both fiddles are erroneous, however in the first one you hand over <C>
3`
to the function show() so that the error occurs in the last iteration and doesn't get visible.
Copy linkTweet thisAlerts:
@VITSUSAJul 22.2019 — @sibert#1606577 What do you want to do exactly? After that we can suggest you more easy ways to solve the existing problem.
Copy linkTweet thisAlerts:
@daveyerwinJul 22.2019 —  list[i].style.display = "none";
`list[i]</C> is a reference to the p element <br/>
it has a style property<br/>
<C>
list[i].id</C> is the value of the p elements Id<br/>
it does not have a style element

<CODE>
`<i>
</i>&lt;html&gt;

&lt;head&gt;
&lt;meta charset="UTF-8"&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;h1&gt;getElementsByClassName Test&lt;/h1&gt;
&lt;p class="step" id="1"&gt;Page 1.&lt;/p&gt;
&lt;p class="step" id="2"&gt;Page 2.&lt;/p&gt;
&lt;p class="step" id="3"&gt;Page 3.&lt;/p&gt;
&lt;form&gt;
&lt;input type="button" value="Hide other steps" onclick="show(3)"&gt;
&lt;/form&gt;

&lt;script&gt;
function show(page) {
var list = document.getElementsByClassName("step");
for (var i = 0; i &lt; list.length; i++) {
alert("loop " + list[i].id)
if (list[i].id == page) {
list[i].style.display = "none";
}
}
}

&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;<i>
</i>
`</CODE>
Edited by site staff: Added inline code tags so that <C>
[i]`
gets visible.
Copy linkTweet thisAlerts:
@daveyerwinJul 22.2019 — @DaveyErwin#1606587

Thank you to the staff

I will be aware of that

in the future


it does not have a style element

I should have said

it does not have a style property
Copy linkTweet thisAlerts:
@sibertauthorJul 22.2019 — > @DaveyErwin#1606587 list[i].style.display = "none";



Thank You!. It is almost exact what I was looking for.

https://jsfiddle.net/ucfmzh7L/6/


> @VITSUSA#1606581 What do you want to do exactly? After that we can suggest you more easy ways to solve the existing problem.



Fiddle above enclosed. Please find a easier way.



Copy linkTweet thisAlerts:
@daveyerwinJul 24.2019 — ``<i>
</i>&lt;head&gt;
&lt;style&gt;
p{display: block;
margin-top: 1em;
margin-bottom: 1em;
margin-left: 0;
margin-right: 0;}
.step{display:none;}
&lt;/style&gt;
&lt;/head&gt;
&lt;p class="step"&gt;Page 1.&lt;/p&gt;
&lt;p class="step"&gt;Page 2.&lt;/p&gt;
&lt;p class="step"&gt;Page 3.&lt;/p&gt;
wwwwww
&lt;script&gt;
(function(){
var steps = document.querySelectorAll('.step');
var page = 0;
steps[page].style.display = 'block';
function next(){
steps[page].style.display = '';
page = (++page &gt; 2) ? 0 : page;
steps[page].style.display = 'block';
}
for(var i=0;i&lt;steps.length;i++){steps[i].onclick=next}
})()
&lt;/script&gt;<i>
</i>
``
×

Success!

Help @sibert 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.25,
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,
)...