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

Subcode match selector.

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

Description

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 (-).

Syntax


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

Parameters


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

Return

N/A.

Attribute Contains Word [attr~="value"] Examplego to top of page Top

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).


$(function(){
  $('#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:

            


go to home page Homepage go to top of page Top