Disable or change anchor tag's href using jQuery


Fani

I have HTML like this:

<div id="StoreLocatorPlaceHoler_ctl07_ctl00_ctl00_pager_ctl00_ctl00_numeric" class="sf_pagerNumeric">
    <a href="http://www.domain.com/store-list">1</a>
    <a href="http://www.domain.com/store-list/page/2">2</a>
    <a href="http://www.domain.com/store-list/page/3">3</a>
    <a href="http://www.domain.com/store-list/page/4">4</a>
    <a href="http://www.domain.com/store-list/page/5">5</a>
    <a href="http://www.domain.com/store-list/page/6">6</a>
    <a href="http://www.domain.com/store-list/page/7">7</a>
    <a href="http://www.domain.com/store-list/page/8">8</a>
    <a href="http://www.domain.com/store-list/page/9">9</a>
    <a href="http://www.domain.com/store-list/page/10">10</a>
</div>

I have no control over the HTML, can't modify it, it's automatically generated by built-in controls in the CMS.

What I want to do with jQuery is to be able to get the href on each anchor tag and modify it, these tags don't have an ID or class associated with them, how can I access these anchor tags in jQuery? Here's what I've tried so far

jQuery(document).ready(function ($) {

    $(".sf_pagerNumeric a").click(function () {
        alert("clicked");

        window.location = "http://www.google.com";
    });
});

However, clicking on the anchor tag doesn't seem to trigger this code, it just uses the default href and redirects. Any help would be greatly appreciated.

EDIT: I don't really want to change it on click, I can do everything right away when the document loads, I need a way to access those anchors.

Miglitazone

Here's how to change the href of each anchor in the div when the document loads.

$(function(){
    $('.sf_pagerNumeric a').each(function(){
        $(this).attr('href', '#changed');
    });
});

violin

Related


Disable or change anchor tag's href using jQuery

Fani I have HTML like this: <div id="StoreLocatorPlaceHoler_ctl07_ctl00_ctl00_pager_ctl00_ctl00_numeric" class="sf_pagerNumeric"> <a href="http://www.domain.com/store-list">1</a> <a href="http://www.domain.com/store-list/page/2">2</a> <a href="http

Can't get anchor tag's href value using jQuery

Sell I'm trying to get the value of an anchor tag hrefusing jQuery , but the thiskeyword is not working as expected. Here is the result of the code in the console: The part of the code I'm having trouble with: $('#container a').click(() => { console.log(th

Can't get anchor tag's href value using jQuery

Sell I'm trying to get the value of an anchor tag hrefusing jQuery , but the thiskeyword is not working as expected. Here is the result of the code in the console: The part of the code I'm having trouble with: $('#container a').click(() => { console.log(th

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 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 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

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

How to change anchor tag's text on click using jquery?

Start I want to change the text of anchor tag on click event using jquery. Anchor Tag: <a id="view_email" href="#"><i class="fa fa-envelope"></i> View Email</a> query code: <script type="text/javascript"> $(document).ready(function(){ $("#view_email").cl

Change href to laravel anchor tag

ABD This is my regular code in blade <a href="<?php echo url();?>/blog/<?php echo $Blog->id;?>">{{ $Blog->BlogTitle }}</a> I want to do something like {{ HTML::link('http://test.com', null, array('id' => 'linkid'))}} I mean I want to do it in a larval way. H

Change href to laravel anchor tag

ABD This is my regular code in blade <a href="<?php echo url();?>/blog/<?php echo $Blog->id;?>">{{ $Blog->BlogTitle }}</a> I want to do something like {{ HTML::link('http://test.com', null, array('id' => 'linkid'))}} I mean I want to do it in a larval way. H

jQuery set special anchor's href to next anchor tag's href

Steve Han I want to find link child <a>and append it to parent <a>. Here is my code. This will return the same link. HTML code <div class="wrap"> <ul class="main-ul"> <li> <a class="child-link" href="link1"></a> </li> </ul> <a class="to-attach"

jQuery set special anchor's href to next anchor tag's href

Steve Han I want to find link child <a>and append it to parent <a>. Here is my code. This will return the same link. HTML code <div class="wrap"> <ul class="main-ul"> <li> <a class="child-link" href="link1"></a> </li> </ul> <a class="to-attach"

How to find href path of anchor tag using jquery?

Vignesh Pichamani How can i get and check if href path contains image path or other link using jquery. E.g: <a href="image.png"><img src="image.png"/></a> In the above example, the anchor text contains the image src, then only the jquery function can find the

How to find href path of anchor tag using jquery?

Vignesh Pichamani How can i get and check if href path contains image path or other link using jquery. E.g: <a href="image.png"><img src="image.png"/></a> In the above example, the anchor text contains the image src, then only the jquery function can find the

Extract anchor tag's href given text using Xpath

username I need to extract url from href attribute given specific text of anchor tag. from scrapy.spider import Spider from scrapy.selector import Selector from nba.items import NBAItem class ESPNSpider(Spider): name = "ESPN" allowed_domains = ["espn.

Extract anchor tag's href given text using Xpath

username I need to extract the url from the href attribute of a specific text given an anchor tag. from scrapy.spider import Spider from scrapy.selector import Selector from nba.items import NBAItem class ESPNSpider(Spider): name = "ESPN" allowed_doma

Extract anchor tag's href given text using Xpath

username I need to extract the url from the href attribute of a specific text given an anchor tag. from scrapy.spider import Spider from scrapy.selector import Selector from nba.items import NBAItem class ESPNSpider(Spider): name = "ESPN" allowed_doma

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready

Get anchor's href value in SVG using jQuery

David M I use jQuery to target anchor elements in an SVG image that I have created to get the value of the href assigned to the anchor element. Then I'm trying to open and close content with the same HTML ID as the anchor element's href. jQuery(document).ready