www.webdeveloper.com
Results 1 to 8 of 8

Thread: element id null issue

  1. #1
    Join Date
    May 2009
    Posts
    3

    element id null issue

    Hello everyone, I'm a newbie to javascript, and would absolutely love if someone could help me understand why it's returning that document.getElementById("footer") is null. What I need this to do is to add the appropriate class to the element with id="footer".

    This is a php document in order to render only specific javascript depending on a variable from a cookie. If you view the link to the full script below, Everything but the problem area is working.

    Code:

    PHP Code:
    #Check to see if cookie has been set
    if(isset($_COOKIE['isExtended']))
        { 
        
    #If it has, set $isExtended var to Cookie var
        
    $isExtended $_COOKIE['isExtended'];
        
    #and echo current value
        
    echo "isExtended = $isExtended ;";
        
    #and set element's rendered class
        
    [COLOR="Red"]if($isExtended == 0
            {
            echo 
    "document.getElementById('footer').setProperty('class','min');";
            }
            else {
            echo 
    "document.getElementById('footer').setProperty('class','max');";
            }[/
    COLOR]
        }
    #If it hasn't, echo default value
    else 

    echo 
    "isExtended = 1 ;";

    Here is the link I'm working on: http://blog.blindmikey.com
    And here is a direct link to the full script: http://blog.blindmikey.com/wp-conten...oterToggle.php

    Thank you to anyone that can explain this to me, sad to admit, I've spent hours on this.
    -mikey

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    setProperty() method is not to be applied upon the elements. It has to handle the CSS styleSheets, something like:
    Code:
    var oStyle = document.styleSheets[0].cssRules[0].style;
    oStyle.setProperty('color','#ff0000','');
    To change a class use DOM 0:
    Code:
    document.getElementById('footer').className='min';

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    I don't think setProperty will work, in all browsers, in this case.
    IE requires className be set:
    Code:
    document.getElementById('footer').className='min';"
    Same problem will occur with getProperty.
    I didn't check if you were using this method with attribute style, but that will not work either.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    May 2009
    Posts
    3
    Thanks for the replies guys, I really appreciate it.
    However, I've tried using that line of code aswell.
    I've used:
    $('footer').setClass('min');
    document.getElementById('footer').className='min';"
    and the line of code posted above.

    Each time I get a return message that 'footer' is null... which I don't understand since there is a div with the id of 'footer'...

    I'm quite lost...

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    $('footer').setClass('min');
    That is not a javascript plain code. $() means nothing in javascript. Nor setClass() method (this should be a custom function placed somewhere else... impossible to know how it works). Do you use a framework? In this case call the framework creators to help you. Usually frameworks bring troubles.
    Last edited by Kor; 05-13-2009 at 04:53 PM.

  6. #6
    Join Date
    May 2009
    Posts
    3
    Hey Kor,
    Thank you for your reply!

    Yes, that particular way of fetching an element by id is from the Mootools framework, however, it's not the framework that I'm having an issue with, as no matter what syntax I use, even the most basic document.getElementById('footer').className='min';" returns that the footer is null.

    The only question I have is why would it return the message of "document.getElementById("footer") is null" ?

    I would greatly appreciate anyone that could help me understand this.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    we need to see the whole picture. Do you have a test page?

  8. #8
    Join Date
    Nov 2008
    Location
    Akron, OH
    Posts
    233

    element null issue

    May very well be a sequence problem.

    Your javascript is attempting to take an action on an element BEFORE it is rendered to the page, hence the element is null.
    javawebdog
    Two things to remember:
    "The only place success comes before work is in the dictionary."
    "It's more than just a matter of survival. It's a matter of sympathy, compassion, passion and style."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles