开发者

Javascript DOM: Pinpointing "input" elements by their type?

I have a need to find and manipulate <input type="checkbox"> from a table. At the moment I have a very spartan function:

function testAjaxCheckBoxes() {
    var table = document.getElementById("ajax_output");
    var nodeList = table.getElementsByTagName("input");
}

That is all I know how to do at the moment. I have a nodeList object of all tags of <input>, but I don't know how to check each one's type or attributes.

I suppose the more gener开发者_C百科al question is how do you view and manipulate attributes of any kind through DOM?


If you have the luxury of targeting Firefox >=3.5 and IE >=8, you can use

document.querySelectorAll("input[type=file]")

to get an array of DOM elements as desired. See more at the MDC documentation.


Once you have an element, you can use the getAttribute, setAttribute, and removeAttribute methods to read, write, and remove attributes.


.getAttribute(attr);

so, if you want to check each item in the nodeList to see if it's a fileupload...

  var nodeList = document.getElementsByTagName("input");
for(item in nodeList) {
    if(nodeList[item].getAttribute("type") == "file") {
     alert("i'm a file");   
    }
    else {
     alert(nodeList[item].getAttribute("type"));
    }
};


If you where to use a library (you should) like JQuery/Mootools etc
It would look like that

var inputs=$('#ajax_output input[type=checkbox]');

OR

var inputs=$('ajax_output').getElements('input[type=checkbox]');`
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜