Attribute Contains Word [attr~="value"]S2C Home « Selectors « Attribute Contains Word [attr~="value"]

Subcode match selector.

Shorthand version  $('[attr~="value"]')


The [attr~="value"] selector, selects all elements with the specified attribute name and a value equal to the specified string or prefixed with that string followed by a hyphen (-).


Signature Description
jQuery('[attr~="value"]')Word match


Parameter Description
attrThe attribute name.
valueThe attribute value which can be either an unquoted single word or a quoted string.



Attribute Contains Word [attr~="value"] ExampleTop

Selects all elements that have the specified attribute name with a value containing the specified word, delimited by spaces.

The following example will select 'input' elements on the page that have a value attribute containing the word 'Turn' and change their background color to orange or yellow (look at the two buttons below).

  $('#btn3').on('click', function() {
    $('input[value~="Turn"]').css('backgroundColor', 'orange');
  $('#btn4').on('click', function() {
    $('input[value~="Turn"]').css('backgroundColor', 'yellow');

Press the buttons below to action the above code: