Monika Lutze Monika Lutze - 2 months ago 7
jQuery Question

catching value of href and using this to replace other href

I have two

div
elements:

<div class="contact-img">
<a href="https://test.de" class="evcms-open-contacts" >
<img src="https://cdn-cache.envivo-connect.com/license/Tw3sZjz8v/p/xckYV8bESwsZ/80x80c/xckYV8bESwsZ.png?v=2.4.84-z3p37YEER-1467970717-yZUA33Hxn" alt="F. Alexander Kep"/>
</a>
</div>
<div class="contact-sm">
<a href="https://www.xing.com/profile/FfAlexander">
</div>


I want to get the
href
from my
contact-sm
class and replace the
href
of my
contact-img
class with Xing link. So if I click on the
contact-img
, the Xing Page of this person should open in a new window.

Thanks for your help

Answer

The best way would be to change your HTML. But if you "need" to have it the way you described then check this jQuery snippet:

($(function(){
    $('.contact-img > a').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();

        $('.contact-sm > a')[0].click();
    })
}))(jQuery);