Select specific anchor link with jQuery


globulin

I have a menu structure like this:

<ul class="menu">
    <li class="parent">
        <a class="menu-link" href="http://url.com/#Id">Id</a>
    </li>
    <li class="parent">
        <a class="menu-link" href="http://url.com/#Id">Id</a>
    </li>
</ul>

and the English version, where the menu looks like this:

<ul class="menu">
    <li class="parent">
        <a class="menu-link" href="http://url.com/en/#Id">Id</a>
    </li>
    <li class="parent">
        <a class="menu-link" href="http://url.com/en/#Id">Id</a>
    </li>
</ul>

Using scrollspy, I'm detecting which part of the page is currently visible. So I can get the ID part of the anchor link. Now how do I choose a specific anchor link?

I tried this:

$('.menu').find(":contains('#" + this.id"')" ).addClass('active');

But it doesn't work (otherwise I wouldn't be here!).

Bhushan Kawadkar

:containsAs part of the check html()/ text()anchor tag, you need to use the hrefanchor tag's attribute selector like this

$('.menu').find("a[href*='#" + this.id + "']" ).addClass('active');

JSFiddle demo

More information about properties contains selectors

Related


Select li tags with specific anchor tag text

Wald Navid I have the following code: <ul id="ulId"> <div> <div> <li class="cModel" style=""><a href="#">4RUNNER</a></li> <li class="cModel" style=""><a href="#">86</a></li> <li class="cModel" style=""><a href

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

jQuery ScrollTo anchor link but class not ID

james How can I modify it so that it scrolls to the class instead of the ID? JS: // Scroll To # Links $('a[href^="#"]').on('click',function (e) { e.preventDefault(); var target = this.hash; var $target = $(target); $('html, body').stop().anim

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

menu javascript or jquery with anchor link

Andre Kessel I found the jquery code with anchor in the link. jQuery (document) .ready (function () { jQuery ("a"). on ('click', function (event) { if (this.hash !== "") { event.preventDefault (); var hash = this.hash;

Get specific text of anchor - jQuery

djasy3 I want to select the specific text of the anchor and modify it at the end. I have many unordered lists on my page and I want to get anchor text with specified href. Here is an example: <ul> <li><a href="vue.php?view_id=6">vue 6</a></li> <li><a hre

jQuery change anchor link text

Charlie I have this anchor link: <li><a class="tab" id="watchTicket" href="#" onclick="CallPage('/section/tickets/view-ticket?action=watch&seq=<?php echo $_GET["seq"]; ?>');"><?php echo ($TicketWatched ? 'Un-Watch' : 'Watch'); ?></a></li> I want to be able to

Select li tags with specific anchor tag text

Wald Navid I have the following code: <ul id="ulId"> <div> <div> <li class="cModel" style=""><a href="#">4RUNNER</a></li> <li class="cModel" style=""><a href="#">86</a></li> <li class="cModel" style=""><a href

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

Add jQuery slideToggle() to oncliked event on anchor link

Jimmy Page I've searched the whole web but haven't managed to find a solution to my problem. I have some anchor links in a fixed header like this: <ul> <li class="dropdown"> <a href="#" class="dropbtn">Tab1</a> <div class="dropdown-content"> <

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

jQuery change id element by clicking on anchor link

Rajnikans I'm trying to change ID element on top click event (anchor link ID). E.g: <div id="top-nav-list"> would be changed to: <div id="new-top-nav-list"> Inside this div:<div id="top-menu">...</div> my jsFiddle: fiddle Any ideas or suggestions? thanks. my

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

Link to a specific anchor on a Laravel page

kash101 How to link to a specific ID in a view using Laravel? my controller: public function services() { return View::make('services.custom_paint', array('pageTitle' => 'Custom Paint'))->with('default_title', $this->default_title); } My route: Route::get('/

jQuery anchor link selector problem

link creator I am trying to use jquery select list anchor links. Although the "list" link does not exist in the page shown in the console output, it seems that the "click" is still being triggered. What causes "list" and "add" to fire? I have the following sim

jQuery ScrollTo anchor link but class not ID

james How can I modify it so that it scrolls to the class instead of the ID? JS: // Scroll To # Links $('a[href^="#"]').on('click',function (e) { e.preventDefault(); var target = this.hash; var $target = $(target); $('html, body').stop().anim

Get link anchor from URL with jQuery

Payson 87 I have a link like <a href="example.com#exampleId>Link</a> I need to put the value "#exampleId" into a variable. How can I do this? I just found a solution without the # sign. Thank you for your help and best regards Girish This is a good trick to g

Select specific anchor link with jQuery

globulin I have a menu structure like this: <ul class="menu"> <li class="parent"> <a class="menu-link" href="http://url.com/#Id">Id</a> </li> <li class="parent"> <a class="menu-link" href="http://url.com/#Id">Id</a> </li> </ul>

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the contents of the specific menu the user was in before the refresh is displayed as active (ie, displayed on

jQuery block link but keep url anchor

grab My link contains href="#video1" I need to prevent click execution $( "#accordion_video a" ).click(function( event ) { event.preventDefault(); }); However, when I do this, I don't get to such#video1 a url, http://example.com/#video1which I need to go ba

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

go to click event jquery anchor link

Marco Ramirez Castro As the description says, I found a lot about smooth scrolling and the location attribute on Javascript, but nothing seems to work. All I'm looking for is to mimic the functionality of the html "a" tag (I don't have to use the html link tag

jQuery change id element by clicking on anchor link

Rajnikans I'm trying to change ID element on top click event (anchor link ID). E.g: <div id="top-nav-list"> would be changed to: <div id="new-top-nav-list"> Inside this div:<div id="top-menu">...</div> my jsFiddle: fiddle Any ideas or suggestions? thanks. my

Get link anchor from URL with jQuery

Payson 87 I have a link like <a href="example.com#exampleId>Link</a> I need to put the value "#exampleId" into a variable. How can I do this? I just found a solution without the # sign. Thank you for your help and best regards Girish This is a good trick to g

Get link anchor from URL with jQuery

Payson 87 I have a link like <a href="example.com#exampleId>Link</a> I need to put the value "#exampleId" into a variable. How can I do this? I just found a solution without the # sign. Thank you for your help and best regards Girish This is a good trick to g

Select anchor tag with specific href using jQuery

Joe Hart Purpose After refreshing the browser, I want the user to stay in the menu/content before the refresh. question After refreshing the browser, the content of the specific menu the user was in before the refresh was displayed as active (ie, displayed on

Select specific anchor link with jQuery

globulin I have a menu structure like this: <ul class="menu"> <li class="parent"> <a class="menu-link" href="http://url.com/#Id">Id</a> </li> <li class="parent"> <a class="menu-link" href="http://url.com/#Id">Id</a> </li> </ul>

Find a specific anchor link from the li to delete

online I am trying to write a code to remove li from ul, but I want to be more specific. I want to find the exact anchor link before deleting it and the li it contains. In the example below, I just want to delete Another link. My code so far looks like this: <