Search code examples
javascriptjqueryurlhreflocation-href

Add anchor to existing url


On a page I want to add a anchor after the existing URL after clicking on a link. So for example my URL in the page I am at the moment is www.google.nl and after clicking the link it needs to be www.google.nl/#anchor. How can I do this?

Second question:

If I click a productlink (WooCommerce product grid) I want to prevent going to that page and have a function that gets the URL of the product and pass it into this function:

jQuery(document).ready(function(){
    jQuery("a").click(function(){
        jQuery("#bestellen").load("dynamic URL here got from the product I clicked on");

    });
});

Solution

  • You can attach an on() click event with jQuery and change the href property with prop() like this:

    $('a.my-selector').on('click', function() {
        $(this).prop('href','https://example.com');
    });