Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with document.getElementById

Status
Not open for further replies.

y2k1981

Programmer
Joined
Aug 2, 2002
Messages
773
Location
IE
can somebody explain to me why this is so?
Code:
var foo = document.getElementById("id1");
alert(foo);
the alert returns object in square brackets. why doesn't it return id1? Obviously the alert doesn't provide any functionality to what I'm trying to do. What I was trying to say was:
Code:
var foo = document.getElementById("id1");
if(foo.style.top < 120)
{
foo.style.top += 20;
}
but when it wasn't working, I put in the alert to see why.  So can somebody answer why the alert doesn't return id1?

Thanks everybody!!
 
maybe the problem is with foo.style.top not being a numeric value. Indeed foo.style.top is probably some numeric value with 'px' after wards. Try parsing it first like so :

var foo = document.getElementById(&quot;id1&quot;);
var fooTop = parseInt(foo.style.top);
if(fooTop < 120)
{
foo.style.top = fooTop + 20;
}

As you can see you can assign a numeric value to foo.style.top but it isn't a numeric value that is given to you when you try to get the value there.

I hope this helps you out. Gary Haran
 
By returning [object], the alert is saying that foo found what you were asking for in your getElementById statement and that it is of type object. What it proves is that &quot;id1&quot; does exist.
To alert the properties of an object try:
alert(foo.style.top);

Hope that makes sense and helps a little for the future. Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
thank you both for your responses. You were spot on Gary, it did return 20px when I entered foo.style.top.

thanks heltel too
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top