Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this very simple html/js code page :

JavaScript
<html>
<head>
   <title>Prova !1</title>
   <script>
      function test (a)
      {
         var v = 5;
         v = !1;		
         return v;
      }

   </script>
</head>
<body>

   <label id='2'> the answer is</label>
   <script>
      window.onload = function ()
      {
         var i ;
         i = document.getElementById('2');
         i[0].innerText += " "+test (0);
      }
   </script>
</body>
</html>



can anyone explain why, no matter what kind of object I use , be it a label or other, the document.getElementbyId('2') always returns null ?
Posted
Updated 13-May-14 5:07am
v3
Comments
Vi(ky 13-May-14 11:03am    
Try this
i = document.getElementById('#2');

and why you pass parameter "a" in test(a), if are not using it
Keith Barrow 13-May-14 11:08am    
Good answer. He might also need to add a name attribute to: <label id='2' name='2'> to support some versions of IE.

Peter is correct. You had problems with...
JavaScript
i[0].innerText += " " + test (0);

Which should be...
JavaScript
i.innerHTML += " " + test(0);


Demo

[Issue] Changing the innerText of a Html Label[^].

Note

As he says, think over the code first, because it is making no sense at all.
 
Share this answer
 
Comments
Peter Leow 13-May-14 12:35pm    
Thanks Tadit, sure 5ed!
Thanks Peter. :)
See my example and comments:
XML
<html>
<head>
   <title>Prova !1</title>
   <script>
      function test (a) // a has never used???
      {
         var v = 5;
         v = !1; // v is always false
         return v;  // returns false
      }

   </script>
</head>
<body>

   <label id='2'> the answer is</label>
   <script>
      window.onload = function ()
      {
         var i ;
         i = document.getElementById('2');
         //i[0].innerText += " "+test (0);
         i.innerHTML += " "+test(0); // the outcome is : the answer is false
      }
   </script>
</body>
</html>

What exactly do you want to do with this weird code?
 
Share this answer
 
v2
Comments
I was going to post the same thing, but found yours. My 5...

I ended up creating a demo to support your answer. Please check.

Thanks,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900