Search code examples
jquerymailto

Add parameter to "mailto:" by Jquery


Is possible to use jQuery to add this parameter to a mailto: link?

?subject=Kontakt%20%7C%20ARCOTEL%20Wimberger%20Wien

This is the HTML:

<a data-ga-category="Direct contact" data-ga-action="Email" href="mailto:wimbergerr@arcotel.com " class="picons-envelope"><span class="pl-2">wimbergerr@arcotel.com</span></a>

I would like to get this output:

<a data-ga-category="Direct contact" data-ga-action="Email" href="mailto:wimbergerr@arcotel.com?subject=Kontakt%20%7C%20ARCOTEL%20Wimberger%20Wien" class="picons-envelope"><span class="pl-2">wimbergerr@arcotel.com</span></a>

Thanks so much!


Solution

  • You can use attr() to update the value of an attribute. Provide a function to the method call which accepts the current value as an argument, and return the new value, like this:

    $('a.picons-envelope').attr('href', (i, href) => href += '?subject=Kontakt%20%7C%20ARCOTEL%20Wimberger%20Wien');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <a data-ga-category="Direct contact" data-ga-action="Email" href="mailto:wimbergerr@arcotel.com" class="picons-envelope"><span class="pl-2">wimbergerr@arcotel.com</span></a>