PDA

View Full Version : Let's see your best getObj() function!


beetle
08-12-2002, 04:03 PM
Hey everyone. I've noticed lately on these boards that alot of people are asking questions in regards to accessing DOM objects. I'd like everyone who can to post their best, most efficient, most compatible (browser/platform-wise) function for retrieving a variable as an object using just the name (or id) of that object. Here's a quick/dirty examplefunction getObj(objName)
{
var obj;
if (document.all)
obj = eval('document.all.'+objName);
else
obj = eval('document.getElementById(\\''+objName+'\\')');
return obj;
}

var formObj = getObj('input1'); And, please, so tab-spacing is preserved, use the vbCode CODE tags (the button labled with the #)

Note to mods: I debated posting this in the 'Post a Javascript' forum. Move it if you see fit, but I though it would receive more pageviews here.

nolachrymose
08-12-2002, 05:02 PM
I never use any functions like this, but there is a really good one at http://www.xs4all.nl/~ppk/js/dhtmloptions.html:

function getObj(name)
{
if (document.getElementById)
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all)
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers)
{
this.obj = getObjNN4(document,name);
this.style = this.obj;
}
}

function getObjNN4(obj,name)
{
var x = obj.layers;
var thereturn;
for (var i=0;i<x.length;i++)
{
if (x[i].id == name)
thereturn = x[i];
else if (x[i].layers.length)
var tmp = getObjNN4(x[i],name);
if (tmp) thereturn = tmp;
}
return thereturn;
}

Happy coding! :)

jkd
08-12-2002, 06:00 PM
function getNode(id) {
return document.layers ? document.layers[id] : (document.getElementById || document.all)(id);
}

Evil coding that will throw strict errors, but no worse than yours, and in much less code.

You can cheat:

return (typeof document.layers != 'undefined') ? document.layers[id] : (document.getElementById || document.all)(id);

and that shouldn't throw any strict errors because the one undefined object in Gecko (document.layers) is checked for properly, and for the short circuit evaluation between DOM and IE, the DOM browsers (such as Gecko) should short circuit evaluate to getElementById, not even evaluating the document.all undefined (in Gecko) reference.

Passin Thru
11-27-2004, 07:38 PM
Well, it's far too late now, but since I was...

document.getElementById = document.getElementById||document.all||document.layers

gettit ?

Passin Thru
11-28-2004, 02:40 PM
Rubbish !

document.getElementById = document.getElementById||document.all||function(id){ return this.layers[id] };

Must learn to slow down while passing.

Roy Sinclair
12-01-2004, 04:08 PM
function getObj(objId)
{ // -- If they haven't got a modern browser then there's no use trying
document.getElementById ? return document.getElementById(objId) : null;
}

Basscyst
12-01-2004, 09:26 PM
Well, 2-3 years ago there probably was. :D

Basscyst

brothercake
12-01-2004, 11:16 PM
Exactly :thumbsup:

if(typeof document.getElementById == 'undefined') { return; }