user57082 user57082 - 1 year ago 90
Javascript Question

How can I add class for the link based on URL andress?

I have a lightbox plugin, which use lightview class for links.
So the usage looks like this:

<a href="" class="lightview">Link for the site</a>

I want add this class (lightview) for everylink which started this url:

This is the important part:

I don't want to add class for links like that:

Because this url will be embedded on my site.

Answer Source

With jQuery you can select all the elements that have an attribute which start with a given value by using the [attribute^=value] selector. Then you would just need to add the lightview class to those elements. It is as simple as:


Here is a demo:

a {
} {
<script src=""></script>
<a href="">Video with right link</a>
<a href="">Video without right link</a>
<a href="">Link to Google</a>
<a href="">Antoher video with right link</a>
<a href="">Another wrong link</a>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download