Using JavaScript to manipulate HTML input (checkbox) elements via type instead of name

后端 未结 7 2122
说谎
说谎 2021-01-04 14:41

I am implementing an HTML form with some checkbox input elements, and I want to have a Select All or DeSelect All button. However, I do not want to rely on the name

7条回答
  •  天命终不由人
    2021-01-04 14:59

    Every input element has an attribute, type, which for checkboxes is "checkbox" so you could try something like this:

    for (var i = 0; i < document.myForm.elements.length; i++) {
        if (document.myForm.elements[i].type == "checkbox") {
            document.myForm.elements[i].checked = true;
        }
    }
    

提交回复
热议问题