Select inputs that doesn't have value attribute with querySelector

后端 未结 2 1838
走了就别回头了
走了就别回头了 2021-01-21 08:55

I\'m using:

document.querySelectorAll(\'input[value=\"\"]\')

But it\'s not selecting inputs that doesn\'t have the attribute value

2条回答
  •  無奈伤痛
    2021-01-21 09:40

    You can use this:

    document.querySelectorAll('input:not([value]):not([value=""])');
    

    get all inputs that doesn't have attribute "value" and the attribute "value" is not blank

    var test = document.querySelectorAll('input:not([value]):not([value=""])');
    for (var i = 0; i < test.length; ++i) {
      test[i].style.borderColor = "red";
    }
    
    
    
    
    

提交回复
热议问题