15

I am quite new with Javascript and I got a problem with document.getElementById() that always returns NULL, and that's driving me nuts.

I have a element in my code and I want to get its coordinates so I can move it.

Here is the code:

<html>
  <head>
    <script type="text/javascript" >
      function MoveIt(obj) {
        alert(obj); // returns "Object HTMLDivElement"
        var xx = document.getElementById("arect");

        if(document.getElementById("arect").value == null) {
          alert('NULL >> ' + xx.value);
        }
        else {
          alert('NOT NULL >>' + xx.value);
        }

        posX = xx.style.scrollTop;
        posY = xx.style.left;
      }
    </script>
  </head>

  <body bgcolor="white" >
    <DIV class="background" id="MyDiv2">  
      <div id="arect" name="arect" class="transbox" onmousedown="MoveIt(this);" >
      </div>
    </div>
  </body>
</html>

The above function MoveIt() always returns NULL

6
  • Please consider cleaning up your code snipit and remove the css as it adds nothing but clutter to you problem. Are you certain that an element with id "arect" exists and are you calling that code after the dom load. Provide the HTML that corresponds with the JS. Commented May 24, 2011 at 14:54
  • 4
    <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> - ??? This is supposed to be a HTML page, riiight? Commented May 24, 2011 at 14:55
  • @thirtydot: That may be the problem here. Commented May 24, 2011 at 14:58
  • 2
    given that you're passing this into the function, and it should equal the object you're trying to get, why do you even need getElementById()? You should be able to just use obj. Commented May 24, 2011 at 14:59
  • You pass your object to your function, but you are not using it :( ... obj = document.getElementById('arect'). Commented May 24, 2011 at 15:04

6 Answers 6

32

The page contents need to be loaded before trying to read them. Try

window.onload = function() {
  // run your script in here
}

Or if you're using jQuery, prefer

$(document).ready(function() {
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

8

You never checked getElementById(...) for NULL.

You checked getElementById(...).value for NULL, and divs don't have a "value".

Also note that you forgot to close that <div /> tag, which is illegal in your XHTML... and used an SVG doctype for some reason. SVG is not HTML.

It's not really clear what you're trying to do here.

3 Comments

Thanks, I forgot to remove SVG doctype. "value" is removed now, but that still does not work
@pasta: What "does not work"? What are you trying to do? Your use of document.getElementById is correct, so something else is up.
That's my point. The way I wrote is apparently "correct" but still have the pb, i.e. document.getElementById("arect") is always null
8

The "arect" element is a <div>, and <div> elements don't have a "value".

Get rid of that bogus SVG doctype too.

4 Comments

Thanks, I forgot to remove SVG doctype. "value" is removed now, but that still does not work
@pasta yes actually it does work - make sure you get rid of all the references to ".value" from the <div> - that includes "xx.value" etc
Thanks a lot it works now, I did some code cleaning and it works ;) Also, do you know how to get the coordinates of the <div> ? I tried with obj.style.left but that returns nothing...
@pasta getting coordinates in general can be tricky - probably should write up a whole separate question for that one :-)
3
if(document.getElementById("arect").value == null){
    alert('NULL >> '+ xx.value);
  }

This code always returns null or error. If you want to see if the object exists, do the following....

if(xx == null)
   alert('Object does not exist');
else 
   alert('Object exists. Inner HTML: ' + xx.innerHTML);

Also, div does not have value. If you want to get the html inside div, use xx.innerHTML

1 Comment

@pasta: What should it be? I can't see any content inside that div.
-1

if the button is set to visisble= false then you cannot get the id of that button on client side. To hide the button use

button1.Style.Add("display","none")-- for visible false

and

button1.Style.Add("display","block")-- for visible true

and even if button is enabled false we cannot get the Id of the button on client side

You can get the id of the button by document.getElementById('<%= button1.ClientID %>'); Or if you set the ClientIDMode="Static" for the control in aspx page you can get it directly by document.getElementById('button1'); Or document.getElementById('MainContent_button1');--- MainContent here is the Id of the contentplaceholder if you have the id of the contenet placeholder different use that id_button1.

2 Comments

Don't sign your posts. Read the FAQ on this.
There is no mention in the question of ASP.NET, but that seems to be where you're approaching this from.
-1

in my case it was because of having this line at the beginning of the jsp/html(whatever) file:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

removing it solved my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.