Click here to Skip to main content
14,358,640 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have this very simple html/js code page :

<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 6: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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

See my example and comments:
<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?
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

Which should be...
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.
Comments
Peter Leow 13-May-14 12:35pm
   
Thanks Tadit, sure 5ed!
   
Thanks Peter. :)

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100