Search code examples
jqueryhrefselector

Select a tag with custom data value and change its href with jquery


I have this piece of codes:

<a href="https://api.whatsapp.com/send?phone=" data-ga-label="666666666" target="_blank"></a>

I want to select this a tag with jquery based-on "data-ga-label" attribute that have a 666666666 value and change its href. How it's possible with jquery? Thank you.


Solution

  • You can add selectors for custom data like below, and then change the href attribute of the link.

    $('a[data-ga-label="666666666"]').attr('href', 'https://stackoverflow.com/');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="https://api.whatsapp.com/send?phone=" data-ga-label="666666666" target="_blank">Link</a>