Selector ‘Attribute Starts With' selects elements that have the specified attribute with a value beginning exactly with a given string, according to the documentation. I would like to to do the exact opposite thing – I would like to select those elements whose value of a specified attribute is at the beginning of given string.
Consider following example:
<a href="/about-us/">About us</a>
<a href="/products/">Products</a>
<a href="/contact/">Contact</a>
Current URL is /products/some-catogory/sub-category/product/?x=y&z=x and I want to highlight the second menu item. What’s the best way to do it?
I’d suggest:
JS Fiddle demo.
Where the
highlightclass-name defines, appropriately enough, the highlighting.This is tested with the following HTML:
Bear in mind this is proofed on JS Fiddle, so it’ll match the the third
aelement (since that’s the one that matches.Edited to offer a slightly more generic solution, in which you can match by a given directroy/subdirectory:
JS Fiddle demo.