Paul T. Rykiel
Paul T. Rykiel

Reputation: 1227

How do I find an anchor element by its href attribute?

I am trying to manipulate the following lines using jQuery:

<a href="#section-one"><div class="section-dots active-section"></div></a>
<a href="#section-two"><div class="section-dots"></div></a>
<a href="#section-three"><div class="section-dots"></div></a>
<a href="#section-four"><div class="section-dots"></div></a>

How can I get the anchor element by an href, for example #section-one, and then update the next element with the class "active-section"?

Upvotes: 0

Views: 44

Answers (2)

Amit Kumar Singh
Amit Kumar Singh

Reputation: 4475

Something like this should work for you. You may have to alter this code a bit.

$(".active-section").removeClass("active-section");
$( "a[href='#section-one']" ).next().addClass( "active-section" );

https://api.jquery.com/category/selectors/

https://api.jquery.com/

Upvotes: 0

Jon Uleis
Jon Uleis

Reputation: 18669

Your question is worded very vaguely, but hopefully this answers it.

how can I get the element by a href ... i.e. #section-one

$('[href="#section-one"]')

and then update the next element class with "active-section"

$('[href="#section-one"]').next().addClass('active-section')

Upvotes: 2

Related Questions