jQuery – Attribute Starts With Selector [name^=”value”]

The jQuery selector selects elements that have the specified attribute with a value beginning exactly with a given string.

Source Code

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

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

Demo

Following example finds all DIV with an attribute ‘attr’ that starts with ‘eng’ and create a border around them.

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

,

No comments yet.

Leave a Reply