Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use an input value attribute as a CSS selector?

Tags:

css

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" /> 
like image 825
blacktie24 Avatar asked May 18 '12 01:05

blacktie24


People also ask

How do you target input values in CSS?

CSS attribute selector is used to targeting an input text fields. The input text fields of type 'text' can be targeting by using input[type=text].

Is attribute a CSS selector?

The CSS attribute selector matches elements based on the presence or value of a given attribute.


1 Answers

Dynamic Values (oh no! D;)

As npup explains in his answer, a simple css rule will only target the attribute value which means that this doesn't cover the actual value of the html node.

JAVASCRIPT TO THE RESCUE!

  • Ugly workaround: http://jsfiddle.net/QmvHL/

Original Answer

Yes it's very possible, using css attribute selectors you can reference input's by their value in this sort of fashion:

input[value="United States"] { color: #F90; }​ 

• jsFiddle example

from the reference

  • [att] Match when the element sets the "att" attribute, whatever the value of the attribute.

  • [att=val] Match when the element's "att" attribute value is exactly "val".

  • [att~=val] Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly "val". If "val" contains white space, it will never represent anything (since the words are separated by spaces). If "val" is the empty string, it will never represent anything either.

  • [att|=val] Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.

  • css attribute selectors reference
like image 65
Michael Zaporozhets Avatar answered Oct 11 '22 04:10

Michael Zaporozhets