最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

getelementsbyname - Javascript Getting specific element (of parent) by name - Stack Overflow

programmeradmin2浏览0评论

I'm using custom tags to define sections in an application, so I have something like this:

<mysection>

   <form>
       <input name="myfield">
   </form>

</mysection>

I'm using the following and able to get the tag (printed to console, everything is groovy)

var parent = document.getElementsByTagName('mysection');

The issue I'm having is finding the child field by name:

var myfield = parent.getElementsByName("myfield");

...as I don't want to pick up on any other 'sections' that might have an input with the name 'myfield'.

EDIT:

var parent = document.getElementsByTagName('mysection')[0];

was suggested and returns to console the section contents, however, getElementsByName throws an error:

Uncaught TypeError: Object #<NodeList> has no method 'getElementsByName' 

I'm using custom tags to define sections in an application, so I have something like this:

<mysection>

   <form>
       <input name="myfield">
   </form>

</mysection>

I'm using the following and able to get the tag (printed to console, everything is groovy)

var parent = document.getElementsByTagName('mysection');

The issue I'm having is finding the child field by name:

var myfield = parent.getElementsByName("myfield");

...as I don't want to pick up on any other 'sections' that might have an input with the name 'myfield'.

EDIT:

var parent = document.getElementsByTagName('mysection')[0];

was suggested and returns to console the section contents, however, getElementsByName throws an error:

Uncaught TypeError: Object #<NodeList> has no method 'getElementsByName' 
Share Improve this question edited Dec 16, 2012 at 23:01 Fluidbyte asked Dec 16, 2012 at 20:59 FluidbyteFluidbyte 5,21010 gold badges49 silver badges79 bronze badges 3
  • By using the id you can get rid of all your problems. – Ariel Chelsău Commented Dec 16, 2012 at 21:01
  • Because this is a large project and I don't want ID overload. – Fluidbyte Commented Dec 16, 2012 at 22:32
  • Then you should also read this thread: stackoverflow.com/questions/14575671/… – Marcus Commented Feb 4, 2013 at 6:56
Add a comment  | 

4 Answers 4

Reset to default 11

Using getElementsByTagName() and getElementsByName() will return a NodeList, you need to get the first element of the list like this:

var parent = document.getElementsByTagName('mysection')[0];
var myfield = parent.getElementsByName("myfield")[0];

Edit

You were correct, getElementsByName is not valid for an element. I am unsure how to localize the functionality of it as you are trying to do. It seems that it will only work for document. You may have to write your own implementation of getElementsByName if you want to use it in a localized scope.

Second Edit

To be nice, I made that implementation for you :D Here it is in all its "glory".

Element.prototype.getElementsByName = function (arg) {
    var returnList = [];
    (function BuildReturn(startPoint) {
        for (var child in startPoint) {
            if (startPoint[child].nodeType != 1) continue; //not an element
            if (startPoint[child].getAttribute("name") == arg) returnList.push(startPoint[child]);
            if (startPoint[child].childNodes.length > 0) {
                BuildReturn(startPoint[child].childNodes);
            }
        }
    })(this.childNodes);
    return returnList;
};
var parent = document.getElementsByTagName('mysection')[0];
var myfield = parent.getElementsByName("myfield")[0];

Small fix

I was incorrectly passing the element and not its children into the recursion. The code above has been edited with the proper argument passed now. See working fiddle: http://jsfiddle.net/js6NP/5/

I actually found a much more simple way to handle this:

document.querySelectorAll('mysection [name="myfield"]');

Here you can see an example where it only modifies the field inside the section specified: http://jsfiddle.net/fluidbyte/kph6H/

qSA supports modern browsers and is compatible down to IE8, Here's a polyfill to support back to IE7: https://gist.github.com/2724353

getElementsByName won't work on a DOM element reference. Use querySelector or querySelectorAll instead. In example:

var parent  = document.getElementsByTagName('mysection')[0];
var myfield = parent.querySelector("[name='myfield']");

Just use an ID instead:

<mysection>
    <form>
         <input name="myfield" id="fieldName">
    </form>
</mysection>
var myfield = document.getElementById("fieldName");

ID's are supposed to be unique on a page. So you shouldn't have trouble accessing the right element.

If you really have to use name/tagname, getElementsByTagName and getElementsByName both always return a array (A empty one if no element was found). you can access the right element, just like you'd access elements in arrays:
document.getElementsByTagName('mysection')[0]; For the first element with tagname mysection.

发布评论

评论列表(0)

  1. 暂无评论