Is it possible to use a CSS selector to target an input that has a specific value?
Example: How can I target the input below based on the value="United States"
<input type="text" value="United States" />
This question is related to
css
In Chrome 72 (2019-02-09) I've discovered that the :in-range
attribute is applied to empty date
inputs, for some reason!
So this works for me: (I added the :not([max]):not([min])
selectors to avoid breaking date inputs that do have a range applied to them:
input[type=date]:not([max]):not([min]):in-range {
color: blue;
}
Screenshot:
Here's a runnable sample:
window.addEventListener( 'DOMContentLoaded', onLoad );_x000D_
_x000D_
function onLoad() {_x000D_
_x000D_
document.getElementById( 'date4' ).value = "2019-02-09";_x000D_
_x000D_
document.getElementById( 'date5' ).value = null;_x000D_
_x000D_
}
_x000D_
label {_x000D_
display: block;_x000D_
margin: 1em;_x000D_
}_x000D_
_x000D_
input[type=date]:not([max]):not([min]):in-range {_x000D_
color: blue;_x000D_
}
_x000D_
<label>_x000D_
<input type="date" id="date1" />_x000D_
Without HTML value=""_x000D_
</label>_x000D_
_x000D_
<label>_x000D_
<input type="date" id="date2" value="2019-02-09" />_x000D_
With HTML value=""_x000D_
</label>_x000D_
_x000D_
<label>_x000D_
<input type="date" id="date3" />_x000D_
Without HTML value="" but modified by user_x000D_
</label>_x000D_
_x000D_
<label>_x000D_
<input type="date" id="date4" />_x000D_
Without HTML value="" but set by script_x000D_
</label>_x000D_
_x000D_
<label>_x000D_
<input type="date" id="date5" value="2019-02-09" />_x000D_
With HTML value="" but cleared by script_x000D_
</label>
_x000D_
You can use Css3 attribute selector or attribute value selector.
/This will make all input whose value is defined to red/
input[value]{
color:red;
}
/This will make conditional selection depending on input value/
input[value="United States"]{
color:red;
}
There are other attribute selector like attribute contains value selector,
input[value="United S"]{
color: red;
}
This will still make any input with United state as red text.
Than we attribute value starts with selector
input[value^='united']{
color: red;
}
Any input text starts with 'united' will have font color red
And the last one is attribute value ends with selector
input[value$='States']{
color:red;
}
Any input value ends with 'States' will have font color red
As mentioned before, you need more than a css selector because it doesn't access the stored value of the node, so javascript is definitely needed. Heres another possible solution:
<style>
input:not([value=""]){
border:2px solid red;
}
</style>
<input type="text" onkeyup="this.setAttribute('value', this.value);"/>
Yes, but note: since the attribute selector (of course) targets the element's attribute, not the DOM node's value property (elem.value), it will not update while the form field is being updated.
Otherwise (with some trickery) I think it could have been used to make a CSS-only substitute for the "placeholder" attribute/functionality. Maybe that's what the OP was after? :)
Following the currently top voted answer, I've found using a dataset / data attribute works well.
//Javascript
const input1 = document.querySelector("#input1");
input1.value = "0.00";
input1.dataset.value = input1.value;
//dataset.value will set "data-value" on the input1 HTML element
//and will be used by CSS targetting the dataset attribute
document.querySelectorAll("input").forEach((input) => {
input.addEventListener("input", function() {
this.dataset.value = this.value;
console.log(this);
})
})
_x000D_
/*CSS*/
input[data-value="0.00"] {
color: red;
}
_x000D_
<!--HTML-->
<div>
<p>Input1 is programmatically set by JavaScript:</p>
<label for="input1">Input 1:</label>
<input id="input1" value="undefined" data-value="undefined">
</div>
<br>
<div>
<p>Try typing 0.00 inside input2:</p>
<label for="input2">Input 2:</label>
<input id="input2" value="undefined" data-value="undefined">
</div>
_x000D_
It is possible, if you're using a browser which supports the CSS :valid
pseudo-class and the pattern
validation attribute on inputs -- which includes most modern browsers except IE9.
For instance, to change the text of an input from black to green when the correct answer is entered:
input {_x000D_
color: black;_x000D_
}_x000D_
input:valid {_x000D_
color: green;_x000D_
}
_x000D_
<p>Which country has fifty states?</p>_x000D_
_x000D_
<input type="text" pattern="^United States$">
_x000D_
Refreshing attribute on events is a better approach than scanning value every tenth of a second...
http://jsfiddle.net/yqdcsqzz/3/
inputElement.onchange = function()
{
this.setAttribute('value', this.value);
};
inputElement.onkeyup = function()
{
this.setAttribute('value', this.value);
};
Source: Stackoverflow.com