[problem] Loop (for) trouble

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 710 Views - Last Post: 02 November 2013 - 04:26 AM

#1 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 13

[problem] Loop (for) trouble

Posted 30 October 2013 - 10:09 AM

Hi everyone, I have a code that when mouse over an element it executes a function and when mouse out an element it executes another function. This function
function out(a) {
        var itens = document.getElementsByClassName("mm");
        var da = document.getElementsByClassName("da");
        var div = document.getElementById("daccess");
        if (a == 0) {
                itens[2].style.backgroundColor = "#0075d3";
                itens[2].style.borderRadius = "5px";
                alert("ok");
        }
        else {
                for (var i = 1; i>itens.length; i++){
                alert(i);
                itens[i].style.backgroundColor = "red";
                itens[i].style.borderRadius = "";
                div.style.display = "none";
                }
        }
}


The problem is that, the "for" loop isn't working, even the condition being false, if I put an alert before the for, it works, if I put any kind of action inside the for, It does not work. I have no idea what is causing it.

Is This A Good Question/Topic? 0
  • +

Replies To: [problem] Loop (for) trouble

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,397
  • Joined: 12-December 12

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 10:27 AM

What errors appear in your browser's console? There are links to the various consoles in my signature.

Does it work in a different browser?

This post has been edited by andrewsw: 30 October 2013 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 10:33 AM

Do you want to loop through the items? In that case your for loop should be
for (var i = 0; i<itens.length; i++){


Was This Post Helpful? 1
  • +
  • -

#4 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 13

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 11:25 AM

Thanks for your replies guys, but in my console nothing appears, and it exactly what I'm doing Casi0o.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 05:12 PM

not quite, compare your code
for (var i = 1; i>itens.length; i++){
with CasiOo’s code:
for (var i = 0; i<itens.length; i++){

your code would only start, if the collection were empty, but then it would run infinitely.
Was This Post Helpful? 0
  • +
  • -

#6 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 13

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 05:19 PM

Oh my gosh Dormilich, how ignorant I were. I really didn't notice that, it fixed my problem, thanks and sorry about.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 30 October 2013 - 05:21 PM

one more tip, DOM access is expensive, so better determine the length beforehand:
for (var i = 0, l = itens.length; i < l; i++) {

or (if the sequence does not matter)
for (var i = itens.length; i--; ) {

Was This Post Helpful? 0
  • +
  • -

#8 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 13

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 07:32 AM

Dormilich are you telling me that I can set a sequence, if is it, please give me a link I want to learn more. Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 07:52 AM

View Postbrunobbn, on 31 October 2013 - 03:32 PM, said:

are you telling me that I can set a sequence

unfortunately, I have no idea what this is supposed to mean.
Was This Post Helpful? 0
  • +
  • -

#10 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 13

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 08:32 AM

or (if the sequence does not matter)
for (var i = itens.length; i--; ) {

[/quote]
You said it, but I think that I understood wrongly.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 08:39 AM

sometimes, you need the indexes to go from 0…n, then you use the former variant. if it also can go from n…0 (i.e. backward), you can use the latter variant (I prefer that–if possible–since it saves me to use one variable)
Was This Post Helpful? 0
  • +
  • -

#12 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 09:32 AM

View PostDormilich, on 31 October 2013 - 12:21 AM, said:

one more tip, DOM access is expensive, so better determine the length beforehand:
for (var i = 0, l = itens.length; i < l; i++) {

or (if the sequence does not matter)
for (var i = itens.length; i--; ) {

Getting the length of an array has nothing to do with DOM access
In most cases it is totally unnecessary to save the length in a local variable, since the performance gain is minimal on an array
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 09:36 AM

View PostCasiOo, on 31 October 2013 - 05:32 PM, said:

Getting the length of an array has nothing to do with DOM access

IF you have an array. but the document.getElementsBy*() methods do NOT return an array, worse (performance-wise) they are live, i.e. each time you call the length property, JS has to check in the document (=> DOM access) what the actual length is.
Was This Post Helpful? 1
  • +
  • -

#14 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,397
  • Joined: 12-December 12

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 09:59 AM

Yes, GetElementsBy* returns an HTMLCollection.

MDN said:

HTMLCollection is an interface representing a generic collection of elements (in document order) and offers methods and properties for traversing the list.

HTMLCollections in the HTML DOM are live; they are automatically updated when the underlying document is changed.

This collection is an array-like object.
Was This Post Helpful? 0
  • +
  • -

#15 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:25 AM

View PostDormilich, on 31 October 2013 - 04:36 PM, said:

View PostCasiOo, on 31 October 2013 - 05:32 PM, said:

Getting the length of an array has nothing to do with DOM access

IF you have an array. but the document.getElementsBy*() methods do NOT return an array, worse (performance-wise) they are live, i.e. each time you call the length property, JS has to check in the document (=> DOM access) what the actual length is.

That seems to be true, I thought it would be an array
Though I do not believe the length is being calculated each time you are doing items.length
It make take more time looking up the value, but the chances of the user noticing is very low if you are not doing a long running task with several thousands of items
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2