jQuery – Attribute Contains Prefix Selector [name|="value"]

The jQuery selector selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<div attr="en">Div 1 (select this)</div>
<div attr="en-US">Div 2 (select this)</div>
<div attr="english">Div 3 (not selected)</div>
<button>Click Here</button>

  
<script>
jQuery("button").click(function () {
	jQuery('div[attr|="en"]').css("border","2px solid #1E8CBE");
});
</script>
</body>
</html>

Demo

Following example finds all div with an attr attribute that is “en” and create a border around them.

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

,

No comments yet.

Leave a Reply