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

后端 未结 9 2225
[愿得一人]
[愿得一人] 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:18

    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);"/>
    
    0 讨论(0)
  • 2020-11-27 17:23

    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);
    };
    
    0 讨论(0)
  • 2020-11-27 17:28

    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);
      })
    })
    /*CSS*/
    
    input[data-value="0.00"] {
      color: red;
    }
    <!--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>

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

    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? :)

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

    Sure, try:

    input[value="United States"]{ color: red; }
    

    jsFiddle example.

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

    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

    0 讨论(0)
提交回复
热议问题