-1

i am using the following javascript code.. i've declared local variables before the starting of all the functions but functions donot execute when called.. but when i replace all of the variables in the function with their content only then it works... what could i be doing wrong.. my code is as follows...

/* variables */
    var dmv = document.getElementById("dmv").style;
    var nomv = document.getElementById("nomv").style;
    var frsv = document.getElementById("frsv").style;
/* end of variables */

function messagesClick() {
    document.getElementById("notifications").style.visibility = 'visible';
    if (dmv.visibility == 'hidden') {
        dmv.visibility = 'visible';
        nomv.visibility = 'hidden';
        frsv.visibility = 'hidden';
    }else{
            dmv.visibility = 'hidden';
    }
}

function notificationsClick() {
    document.getElementById("notifications").style.visibility = 'visible';
    if (nomv.visibility == 'hidden') {
        nomv.visibility = 'visible';
        dmv.visibility = 'hidden';
        frsv.visibility = 'hidden';
    }else{
            nomv.visibility = 'hidden';
    }
}

function friendRequestClick() {
    document.getElementById("notifications").style.visibility = 'visible';
    if (frsv.visibility == 'hidden') {
        frsv.visibility = 'visible';
        dmv.visibility = 'hidden';
        nomv.visibility = 'hidden';
    }else{
            frsv.visibility = 'hidden';
    }
}

any help would be appreciated.. thanks in advance.. :)

Irvin Dominin
  • 29,799
  • 9
  • 75
  • 107
Sajeeb
  • 369
  • 1
  • 8
  • 26
  • 3
    Are you trying to access the DOM before it's fully loaded? – Mathletics Dec 17 '13 at 20:07
  • Here is a discussion about how to check if document is ready without jQuery. http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – Kris Hollenbeck Dec 17 '13 at 20:16
  • 1
    For the guy who voted negative: aren't we supposed to explain why the negative vote? If you think that the question is "bad" you are supposed to explain why so new users can improve their questions. – Roimer Dec 17 '13 at 20:38

1 Answers1

1

You have to wait until the elements exist before you can access them.

You can wait until you need the values the first time to get them:

var dmv, nomv, frsv;
var gotStyles = false;

function getStyles() {
    if (!gotStyles) {
        gotStyles = true;
        dmv = document.getElementById("dmv").style;
        nomv = document.getElementById("nomv").style;
        frsv = document.getElementById("frsv").style;
    }
}

function messagesClick() {
    getStyles();
    document.getElementById("notifications").style.visibility = 'visible';
    if (dmv.visibility == 'hidden') {
        dmv.visibility = 'visible';
        nomv.visibility = 'hidden';
        frsv.visibility = 'hidden';
    }else{
            dmv.visibility = 'hidden';
    }
}

function notificationsClick() {
    getStyles();
    document.getElementById("notifications").style.visibility = 'visible';
    if (nomv.visibility == 'hidden') {
        nomv.visibility = 'visible';
        dmv.visibility = 'hidden';
        frsv.visibility = 'hidden';
    }else{
            nomv.visibility = 'hidden';
    }
}

function friendRequestClick() {
    getStyles();
    document.getElementById("notifications").style.visibility = 'visible';
    if (frsv.visibility == 'hidden') {
        frsv.visibility = 'visible';
        dmv.visibility = 'hidden';
        nomv.visibility = 'hidden';
    }else{
            frsv.visibility = 'hidden';
    }
}
Guffa
  • 640,220
  • 96
  • 678
  • 956