none
document.getElementById('p1') always return null ? why ?

    Question

  • document.getElementById('p1') always return null ? why ? METRO APP, not IE ok ?..... i just had a feeling someone will mistaken that i am not making a metro app. Sigh. Sorry for the pessimism.

    Monday, June 18, 2012 2:17 PM

Answers

  • ahh i found the problem, turns out i need to add the code inside  app.onactivated = function (args) {} instead of after app.start().

    Monday, June 18, 2012 2:31 PM

All replies

  • Can you share an HTML and a WinJS snippet?
    Monday, June 18, 2012 2:20 PM
  • When are you calling it?  Is the DOM ready to be queried?  Try using the ready function if this on initialization.

    WinJS.Utilities.ready(function(){
    
    	var myElement = document.getElementById("p1");
    
    });



    Monday, June 18, 2012 2:22 PM
  • the HTML:

    ==

    <body>

    <p id="p1">Hahahaha</p>

    </body>

    ==

    The Default.js

    ==

    // typical js file

    app.start();

    document.getElementById('p1').innerHTML = 'Bla bla bla';

    ==

    DOES NOT WORK. document.getElementById() returns null.

    Unhandled exception at line 32, column 5 in ms-appx://1635a84c-7b10-4678-8389-34aefae706f0/default.js

    0x800a138f - JavaScript runtime error: Unable to set property 'innerHTML' of undefined or null reference

    Monday, June 18, 2012 2:29 PM
  • The DOM is not ready.  See my previous reply.
    Monday, June 18, 2012 2:30 PM
  • ahh i found the problem, turns out i need to add the code inside  app.onactivated = function (args) {} instead of after app.start().

    Monday, June 18, 2012 2:31 PM
  • jrboddie, harelquin. thanks for the info though, i do appreciates it.
    Monday, June 18, 2012 2:43 PM