开发者

What is the use of grabFormSelects() within a javascript form?

开发者 https://www.devze.com 2023-02-23 04:24 出处:网络
I am a javascript learner and I got a code writton by someone. A portion of it goes like this: var selects = grabFormSelects(holder, \"valueList\");

I am a javascript learner and I got a code writton by someone. A portion of it goes like this:

var selects = grabFormSelects(holder, "valueList");

It grabs a reffers to a class in a select within a form. Check: http://jsfiddle.net/YS6mm/12/

I searched the internet, and nowhere do I find info about "grabFormSelects". What is it?

[edit] I can't even create a tag at stackoverlfow from th开发者_开发百科is because it's new. Now Im even more curious!


It's a function; it's right there in your jsfiddle:

function grabFormSelects(parent, class_name)
{
    //make new array to hold nodes
    var nodes = [];
    for(var i=0;i<parent.childNodes.length;i++)
    {
        var node = parent.childNodes[i];
        //filter out any node that isn't an element node and doesn't have the class name we're looking for
        if(node.nodeType === 1 && node.className === class_name)
        {
            nodes.push(node);
        }   
    }
    return nodes;
}

What it does is look for DOM nodes in some container (directly in the container, as 1st generation children) that have a "class" attribute matching some string. It returns an array of those nodes.

In the page, you'll note that all the <select> elements are given the class, "valueList". That's what's passed in to the "grabFormSelects()" function, so it gets all the <select> DOM nodes.

edit — for clarification, that's just a plain old function somebody typed in with their own plain old hands. It's not any sort of standard feature, or standard approach to the problem. It's not particularly bad or anything, but you're not going to find blog posts about it or anything like that.


you said, the code is written by someone, and he wants to give "grabFormSelects" name to the function.

0

精彩评论

暂无评论...
验证码 换一张
取 消