最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select inputs that doesn't have value attribute with querySelector - Stack Overflow

programmeradmin4浏览0评论

I'm using:

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

But it's not selecting inputs that doesn't have the attribute value.
How can I select all inputs that doesn't have a value '', and that doesn't have the attribute value?

I would like to do that without jQuery and without any additional function, would that be possible only using querySelectorAll?

I'm using:

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

But it's not selecting inputs that doesn't have the attribute value.
How can I select all inputs that doesn't have a value '', and that doesn't have the attribute value?

I would like to do that without jQuery and without any additional function, would that be possible only using querySelectorAll?

Share Improve this question asked Oct 31, 2014 at 19:34 BernardoLimaBernardoLima 1,1412 gold badges18 silver badges35 bronze badges 1
  • That's a bit confusing. You want to select those inputs which have values, or the ones that don't have? – LcSalazar Commented Oct 31, 2014 at 19:38
Add a comment  | 

2 Answers 2

Reset to default 16

Try

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

This should return all the input that don't have the value attribute.

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";
}
<input type="text" />
<input type="text" value="2" />
<input type="text" />
<input type="text" value="" />
<input type="text" />

发布评论

评论列表(0)

  1. 暂无评论