jQuery – Attribute Contains Word Selector [name~=”value”]

The jQuery selector selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

Source Code

<!DOCTYPE html>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
<div attr="en">Div 1 (select this)</div>
<div attr="en-US">Div 2 (not selected)</div>
<div attr="en Us">Div 3 (select this)</div>
<div attr="english">Div 4 (not selected)</div>
<button>Click Here</button>
jQuery("button").click(function () {
	jQuery('div[attr~="en"]').css("border","2px solid #1E8CBE");


Following example finds all DIVS with a ‘attr’ attribute that contains the word ‘en’ and create a border around them.

Div 1 (select this)
Div 2 (not selected)
Div 3 (select this)
Div 4 (not selected)


No comments yet.

Leave a Reply