Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

dom

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 general question is how do you view and manipulate attributes of any kind through DOM?

like image 518
Danedo Avatar asked Sep 14 '26 22:09

Danedo


2 Answers

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.

like image 71
Domenic Avatar answered Sep 16 '26 10:09

Domenic


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

like image 36
icktoofay Avatar answered Sep 16 '26 10:09

icktoofay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!