The Netlobo logo - a Nevada desert landscape

Comments for Using Javascript to get an Element by ID

< Back to the article

15 comments for this article.

RSS Feed Icon Subscribe to the comments for this article

Posted: 2007-07-30 16:22:06 by zoogies
Very nice...I get antsy about using javascript because of some of the odd cross browser issues that happen, so this will be a huge help. Thanks!
Posted: 2007-08-27 19:42:35 by bababa
Spelling error: "getElements" should be "getElement".
Posted: 2007-08-27 19:56:39 by lobo235 - Netlobo Staff Member
Thanks for catching that. We have now updated the article with the spelling change.
Posted: 2007-09-04 15:47:56 by SamAllen
Newbie here - can you tell me how I would instigate this in a page ( example). I am trying to return an elements id. Testing it with the alert. But return var always comes back null. Please help me aka the new kid :


function returnObjById( id )
if (document.getElementById)
var returnVar = document.getElementById(id);
else if (document.all)
var returnVar = document.all[id];
else if (document.layers)
var returnVar = document.layers[id];
return returnVar;
Posted: 2007-09-04 15:50:38 by lobo235 - Netlobo Staff Member
SamAllen, in the line where you are calling alert(returnVar); it is trying to alert the object representing the element on your page. Instead you would need to do alert(; which would alert the id of the element.

I hope this helps. If you still have trouble with this feel free to use our contact form to send us another message and we can help you further.
Posted: 2007-09-11 03:54:15 by manchumahara
very help full. thanx a lot
Posted: 2007-09-26 12:18:17 by pete
You might find this site interesting too:

It points out that the getElementById method in IE6 and IE7 is *STILL* broken! and will return the wrong objects (if you aren't very careful)

A workaround for the bug is also provided at the end of the article.
Posted: 2007-11-28 23:42:33 by shaira
I want to identify all the javascript elements by Id.
How do i do that?
Posted: 2008-03-19 16:53:23 by CJohnson
var ThisIsYourElementID = document.GetElementByID('ThisIstheIDofYourElement');
Posted: 2008-07-16 10:30:43 by SoumenTrivedi
I have tested this code, and it seems to return the id for the tag name and field name passed. This piece of code has helped resolving the issues with fetching element ids

function getElementId(_tagName,_fieldName) {
	var elements = document.getElementsByTagName(_tagName);
	if (elements != null && elements.length != 0) {
		for (i=0, x=elements.length; i<x; i++) {
			if(elements[i].id.match(_fieldName) != null) {
			return elements[i].id;

For null checks as well the code which i have used and I feel it can be of help to this forum:

function IsNotEmpty(aTextField) {
	if (aTextField != null) {
		if (( aTextField.value == 'undefined') || (aTextField.value == null)) {
			return true;
		} else {
			return false;
	} else {
		return true;
Posted: 2008-10-23 18:59:37 by db
Love your work :)
Posted: 2008-12-29 09:14:16 by JustMe
Even simpler (shorter):
var d=document;
function o(id){
    return d.getElementById?d.getElementById(id):(d.all?d.all[id]:(d.layers?d.layers[id]:null));
Posted: 2009-01-13 12:32:42 by Ofer
Thanks, i always get the js functions wrong.
<a href="">Ofer</a>
Posted: 2009-08-06 07:10:31 by Sandy
Hi i have a small problem.. I display few details fetching from the database. Now i want to populate those details into another page where i have the fields. can you plz guide me how do i go upon this..
Thanks in advance..
Posted: 2010-04-26 11:10:54 by Rajneesh
Thsnk for cool information .I love javascript.

RSS Feed Icon Subscribe to the comments for this article

Post your comment for the Using Javascript to get an Element by ID article:

Name (required) (letters and numbers only):
Email (required) (will not be published):
Website (include http://):
Comment (required): (HTML tags allowed: pre, strong, em, b, i)