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

后端 未结 9 2226
[愿得一人]
[愿得一人] 2020-11-27 17:07

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

相关标签:
9条回答
  • 2020-11-27 17:39

    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 );
    
    function onLoad() {
        
        document.getElementById( 'date4' ).value = "2019-02-09";
        
        document.getElementById( 'date5' ).value = null;
        
    }
    label {
        display: block;
        margin: 1em;
    }
    
    input[type=date]:not([max]):not([min]):in-range {
        color: blue;
    }
    <label>
        <input type="date" id="date1" />
        Without HTML value=""
    </label>
        
    <label>
        <input type="date" id="date2" value="2019-02-09" />
        With HTML value=""
    </label>
    
    <label>
        <input type="date" id="date3" />
        Without HTML value="" but modified by user
    </label>
        
    <label>
        <input type="date" id="date4" />
        Without HTML value="" but set by script
    </label>
        
    <label>
        <input type="date" id="date5" value="2019-02-09" />
        With HTML value="" but cleared by script
    </label>

    0 讨论(0)
  • 2020-11-27 17:42

    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 {
      color: black;
    }
    input:valid {
      color: green;
    }
    <p>Which country has fifty states?</p>
    
    <input type="text" pattern="^United States$">

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题