Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Let's see your best getObj() function!

    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 example
    Code:
    function 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.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I never use any functions like this, but there is a really good one at http://www.xs4all.nl/~ppk/js/dhtmloptions.html:

    Code:
    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!

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #4
    New Coder
    Join Date
    Nov 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, it's far too late now, but since I was...

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

    gettit ?

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rubbish !

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

    Must learn to slow down while passing.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function getObj(objId)
    { // -- If they haven't got a modern browser then there's no use trying 
    document.getElementById ? return document.getElementById(objId) : null;
    }
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #7
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Well, 2-3 years ago there probably was.

    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Exactly
    Code:
    if(typeof document.getElementById == 'undefined') { return; }
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •