admin管理员组文章数量:1296485
I have multiple inputs I want to discriminate according to whether the user enters a value in it or not.
<input type="text" class="foo"> <br/>
<input type="text" class="foo" value = "D"> <br/>
//and so on..
<button onclick="calculate()">Hightlight</button>
The code I wrote only works with attribute values, instead of detecting "manually" typed values before launching the function :
function calculate() {
var allinputs = document.querySelectorAll('input[value][type="text"]:not([value=""])');
var myLength = allinputs.length;
for (var i = 0; i < myLength; ++i) {
allinputs[i].style.backgroundColor = "lime";
}
}
I only want to detect the inputs in which the user typed something when he submits it through the highlight button. Is there a way to do it in pure javascript ?
Here is my fiddle : /
Thanks for your help
I have multiple inputs I want to discriminate according to whether the user enters a value in it or not.
<input type="text" class="foo"> <br/>
<input type="text" class="foo" value = "D"> <br/>
//and so on..
<button onclick="calculate()">Hightlight</button>
The code I wrote only works with attribute values, instead of detecting "manually" typed values before launching the function :
function calculate() {
var allinputs = document.querySelectorAll('input[value][type="text"]:not([value=""])');
var myLength = allinputs.length;
for (var i = 0; i < myLength; ++i) {
allinputs[i].style.backgroundColor = "lime";
}
}
I only want to detect the inputs in which the user typed something when he submits it through the highlight button. Is there a way to do it in pure javascript ?
Here is my fiddle : https://jsfiddle/Lau1989/Lytwyn8s/
Thanks for your help
Share Improve this question asked Oct 7, 2016 at 8:19 LauLau 1792 gold badges4 silver badges13 bronze badges2 Answers
Reset to default 3It's not possible to select empty inputs using CSS selectors (see Matching an empty input box using CSS), but you can achieve what you want by testing the length of the value before applying the style:
function calculate() {
var allinputs = document.querySelectorAll('input[type="text"]');
var myLength = allinputs.length;
var input;
for (var i = 0; i < myLength; ++i) {
input = allinputs[i];
if (input.value) {
input.style.backgroundColor = "lime";
}
}
}
You need to check value like this
function calculate() {
var allinputs = document.querySelectorAll('input[type="text"]');
var myLength = allinputs.length;
for (var i = 0; i < myLength; ++i) {
if (allinputs[i].value == '') {
allinputs[i].style.backgroundColor = "lime";
}
}
}
<input type="text" class="foo">
<br/>
<input type="text" class="foo" value="D">
<br/>
<input type="text" class="foo" value="G">
<button onclick="calculate()">Hightlight</button>
<br/>
<br/>
<div id="output"></div>
See also JsFiddle link
And if you want to remove lime background when user fills input and clicks again you can add else
statement.
function calculate() {
var allinputs = document.querySelectorAll('input[type="text"]');
var myLength = allinputs.length;
for (var i = 0; i < myLength; ++i) {
if (allinputs[i].value == '') {
allinputs[i].style.backgroundColor = "lime";
}else {
allinputs[i].style.backgroundColor = "white";
//allinputs[i].removeAttribute("style") or that
}
}
}
<input type="text" class="foo">
<br/>
<input type="text" class="foo" value="D">
<br/>
<input type="text" class="foo" value="G">
<button onclick="calculate()">Hightlight</button>
<br/>
<br/>
<div id="output"></div>
UPDATE
You need that
function calculate() {
var allinputs = document.querySelectorAll('input[type="text"]');
var myLength = allinputs.length;
for (var i = 0; i < myLength; ++i) {
if (!allinputs[i].hasAttribute("value")) {
if(allinputs[i].value !== '') {
allinputs[i].style.backgroundColor = "lime";
}else {
allinputs[i].removeAttribute("style");
}
}
}
}
<input type="text" class="foo">
<br/>
<input type="text" class="foo" value="D">
<br/>
<input type="text" class="foo" value="G">
<button onclick="calculate()">Hightlight</button>
<br/>
<br/>
<div id="output"></div>
本文标签: javascriptquerySelectorAll detecting value in inputStack Overflow
版权声明:本文标题:javascript - querySelectorAll detecting value in input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639136a2389813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论