Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 149,620 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!





Partial fix for IE's getElementsByName()

While trying to make a "reloading" script for use with my transparent HTML/CSS backgrounds I came across a problem with IE. I couldn't use object.getElementsByName()! So I came up with this solution. Granted it's limited to one kind of tag right now, but it should make coding standard (X)HTML a little easier.

Submitted By: snoj
Actions:
Rating:
Views: 21,389

Language: JavaScript

Last Modified: November 11, 2005

Snippet


  1. function getElementsByName_iefix(tag, name) {
  2.      
  3.      var elem = document.getElementsByTagName(tag);
  4.      var arr = new Array();
  5.      for(i = 0,iarr = 0; i < elem.length; i++) {
  6.           att = elem[i].getAttribute("name");
  7.           if(att == name) {
  8.                arr[iarr] = elem[i];
  9.                iarr++;
  10.           }
  11.      }
  12.      return arr;
  13. }

Copy & Paste


Comments


dimi3 2008-10-30 08:23:34

How about something like this? beware though, as it recursively parses the entire DOM tree, so it could cause considerable overhead on large or deeply nested pages document.getElementsByName = function(name){ var out = []; function getElementsByNameDelegate(elem, eName, results){ if(elem.name && elem.name == eName) results.push(elem); for(var i=0; i

dimi3 2008-10-30 08:24:52

elem.childNodes.length;i++) getElementsByNameDelegate(elem.childNodes, eName, results); } getElementsByNameDelegate(document, name, out); return out; } a beter solution might be to make the method iterative instead of recursive. Please note this method does not work for HTML elements that don't support the name attribute such as div's and span's (at least it didn't in FF 3 when i tested this). Another use for this might be to create a getElementsByClassName function to search for elements using a certain css class (just change .name to .className)

dimi3 2008-10-30 08:25:38

My bad for the strange markup.. first comment i ever posted here, didn't know how it was going to turn out..


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month