Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementsByName in IE7

I have some code doing this :

 var changes = document.getElementsByName(from);
 for (var c=0; c<changes.length; c++) {
   var ch = changes[c];
   var current = new String(ch.innerHTML);
   etc.
 }

This works fine in FF and Chrome but not in IE7. Presumably because getElementsByName isn't working in IE. What's the best workaround?

like image 210
sa386 Avatar asked Nov 10 '08 18:11

sa386


People also ask

How do you get getElementsByName value?

JavaScript getElementsByName() example How it works: First, select the submit button by its id btnRate using the getElementById() method. Second, listen to the click event of the submit button. Third, get all the radio buttons using the getElementsByName() and show the selected value in the output element.

What is the getElementsByName () method commonly used to obtain?

The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.

What is the difference between getElementsByName and getElementsByTagName?

getElementsByName fetch all the elements with given name and returns list of elements. getElementsByTagName fetch all the elements with given Tag and returns list of elements.

How do I find the input name of an element?

Use the querySelector() method to get an element by a name attribute, e.g. document. querySelector('[name="first_name"]') . The method returns the first element in the DOM that matches the provided selector. If no element matches the selector, null is returned.


2 Answers

In case you don't know why this isn't working in IE, here is the MSDN documentation on that function:

When you use the getElementsByName method, all elements in the document that have the specified NAME attribute or ID attribute value are returned.

Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection; therefore, this method cannot be used to retrieve custom tags by name.

Firefox allows getElementsByName() to retrieve elements that use a NAME expando, which is why it works. Whether or not that is a Good Thing™ may be up for debate, but that is the reality of it.

So, one option is to use the getAttribute() DOM method to ask for the NAME attribute and then test the value to see if it is what you want, and if so, add it to an array. This would require, however, that you iterate over all of the nodes in the page or at least within a subsection, which wouldn't be the most efficient. You could constrain that list beforehand by using something like getElementsByTagName() perhaps.

Another way to do this, if you are in control of the HTML of the page, is to give all of the elements of interest an Id that varies only by number, e.g.:

<div id="Change0">...</div>
<div id="Change1">...</div>
<div id="Change2">...</div>
<div id="Change3">...</div>

And then have JavaScript like this:

// assumes consecutive numbering, starting at 0
function getElementsByModifiedId(baseIdentifier) {
    var allWantedElements = [];
    var idMod = 0;
    while(document.getElementById(baseIdentifier + idMod)) { // will stop when it can't find any more
        allWantedElements.push(document.getElementById(baseIdentifier + idMod++));
    }
    return allWantedElements;
}

// call it like so:
var changes = getElementsByModifiedId("Change");

That is a hack, of course, but it would do the job you need and not be too inefficient compare to some other hacks.

If you are using a JavaScript framework/toolkit of some kind, you options are much better, but I don't have time to get into those specifics unless you indicate you are using one. Personally, I don't know how people live without one, they save so much time, effort and frustration that you can't afford not to use one.

like image 103
Jason Bunting Avatar answered Sep 30 '22 18:09

Jason Bunting


There are a couple of problems:

  1. IE is indeed confusing id="" with name=""
  2. name="" isn't allowed on <span>

To fix, I suggest:

  1. Change all the name="" to class=""
  2. Change your code like this:

-

var changes = document.getElementById('text').getElementsByTagName('span');
for (var c=0; c<changes.length; c++) {
 var ch = changes[c];

 if (ch.className != from)
continue;

 var current = new String(ch.innerHTML);
like image 23
Greg Avatar answered Sep 30 '22 20:09

Greg