-7
$(this).find("a[href^='/']")

I'm particularly interested in knowing this part "a[href^='/']"

Sheraz Arshad
  • 191
  • 1
  • 10

3 Answers3

2

jQuery uses CSS selectors. a[^='/'] will select all <a> whose href attribute starts with / which are children of whatever the this is.

See it in action:

$("ul").each(function () {
  $(this).find("a[href^='/']").addClass("selected");
});
.selected {
   background-color: lime;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li><a href="http://www.google.com">Will not be selected</a></li>
    <li><a href="/example">Will be selected</a></li> 
</ul>

<ul>
    <li><a href="/example">Yep</a></li> 
    <li><a href="http://www.google.com">Nope</a></li>
</ul>
iblamefish
  • 4,631
  • 3
  • 32
  • 44
1

This particular code is CSS Attribute Selector to find <a> elements with an href attribute value that starts with a /.

More here: https://api.jquery.com/attribute-starts-with-selector/

brennanyoung
  • 5,858
  • 3
  • 23
  • 41
pwolaq
  • 6,123
  • 18
  • 45
0

It looks for a link to the href beginning with /.

jar3d
  • 103
  • 9