Mamadou Mamadou - 3 months ago 14
HTML Question

Add a class in an anchor tag within a div with jquery

does anyone know how Can I add a class in the A tag:

<a href="/souscription/digital-vie">Subscribe</a>


Here is my html:

<div class="module" id="prod-subscription">
<div class="entity entity-bean bean-ap-bl-instruct contextual-links-region block container">
<div class="contextual-links-wrapper contextual-links-processed">
<a class="contextual-links-trigger" href="#">Configure</a>
<ul class="contextual-links">
<li class="bean- first last">
<a href="/block/souscription/edit?destination=node/3719"> bloc</a>
</li>
</ul>
</div>
<div class="row">
<div class="col-md-3">
<h2>simple<span>in 3 </span></h2>
</div>
<div class="col-md-6">
<ol>
<li>
<span>1</span><p>text</p>
</li>
<li>
<span>2</span><p>texte testx</p>
</li>
<li>
<span>3</span><p>and text</p>
</li>
</ol>
</div>
<div class="col-md-3">
<a href="/souscription/digital-vie">Subscribe</a>
</div>
</div>
</div>
</div>

Answer

you can use

$('a[href="/souscription/digital-vie"]').addClass('classHere');

or

$('div#prod-subscription a[href="/souscription/digital-vie"]').addClass('classHere');

or

$('a[href="/souscription/digital-vie"]:contains("Subscribe")').addClass('classHere');

$('a[href="/souscription/digital-vie"]:contains("Subscribe")').addClass('classHere');
.classHere{
  background : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="module" id="prod-subscription">
    <div class="entity entity-bean bean-ap-bl-instruct contextual-links-region block container">
        <div class="contextual-links-wrapper contextual-links-processed">
            <a class="contextual-links-trigger" href="#">Configure</a>
            <ul class="contextual-links">
                <li class="bean- first last">
                    <a href="/block/souscription/edit?destination=node/3719"> bloc</a>
                </li>
            </ul>
        </div>    
        <div class="row">
            <div class="col-md-3">
                <h2>simple<span>in 3 </span></h2>
            </div>
            <div class="col-md-6">
                <ol>
                    <li>
                        <span>1</span><p>text</p>
                    </li>
                    <li>
                        <span>2</span><p>texte testx</p>
                    </li>
                    <li>
                        <span>3</span><p>and text</p>
                    </li>
                </ol>
            </div>
            <div class="col-md-3">
                <a href="/souscription/digital-vie">Subscribe</a>
            </div>
        </div>
    </div>
</div>

Comments