0

I have a problem with my JavaScript function, in the "for" part it doesn't recognize the HTML elements when I use i to refer to the list position, but when I use [0] or [1], for example, it does recognize it. So there must be a problem with the loop part but I can't figure out what is it, here is the code:

(function () {
    "use strict";
    window.animacion_click_menu = function (id) {
        var i;
        var menu = document.getElementById('menu').getElementsByTagName('LI');
        var bloqueActual = document.getElementById(id);
        for (i = 0; i <= menu.length; i++) {      //recorre los LI devolviendolos a su posicion original
            menu[i].style.marginLeft = -40;
            menu[i].style.opacity = 1;
        }
        bloqueActual.style.marginLeft = 200;
        bloqueActual.style.opacity = 0;
    };
})(); 

and here's my html:

<head>
    <meta charset="UTF-8">
    <title>Mario Luque Marchena</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/estilos.css">
</head>

<body>
    <center>
        <h1 class="titulo">Bienvenid@</h1>
    </center>
    <div id="main-screen">
        <ul id="menu">
            <center>
                <li id="sobremi" onclick="window.animacion_click_menu('sobremi');">Sobre mi</li>
                <li id="portafolios" onclick="animacion_click_menu('portafolios');">Portafolios</li>
                <li id="animacion" onclick="animacion_click_menu('animacion');">Animacion</li>
                <li id="back-end" style="border-bottom-style: dashed;" onclick="animacion_click_menu('back-end');">Back-End</li>
            </center>
        </ul>
    </div>  
    <script type="text/javascript" src="js/animaciones.js"></script>
</body>

if you have any suggestions to make the code better, are welcome too, i'm learning to code. thank you!, and sorry for the bad english in case it was

1
  • 1
    You want to use i < menu.length, not <=. Commented Feb 3, 2016 at 13:11

2 Answers 2

2

Your error is really on the for loop. Take a look on:

for (i = 0; i <= menu.length; i++) {

it should be:

for (i = 0; i <= menu.length-1; i++) {

Otherwise, it will try to iterate from 0 to 5 while your menu array has only 4 items. The result is that in the last iteration, when you try to access the element menu with the inexistent index (menu[5]) you get the error:

Uncaught TypeError: Cannot read property 'style' of undefined

Other possibility to overcome this is to change <= to < and work with the loop as:

for (i = 0; i < menu.length; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was the error i had! it was such a simple error hahaha i changed the <= for < and it worked! thank you very much!
-1

use window.onload() or

$('document').ready(function(){
  //Put your code here
})

I think your code is getting executed before DOM creation.

2 Comments

The error was in the loop, i solved it, but what you're saying could happen too, i will use one of those, thank you!
$('document').ready(function(){...}) is equivalent to $(function(){...}), for those who are not aware.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.