JQuery - Next div when clicking on a link

1

I have 2 divs and when I click inside a link within the first div I need to get the value of the 2nd div, how do I do this?

HTML:

<div class="vei">
    <span class="download"><a target="_blank" href='teste2.php'>download</a></span>
</div>
<div class="titulo"><a target="_blank" href='teste.php'>Teste</a></div>

JS (I tried this but it did not work):

$( ".download a" ).click(function(e){
    $( ".download a" ).html($( ".titulo a" ).text(););
});

The "a" has to become "Test" and no longer "download"!

    
asked by anonymous 03.10.2016 / 21:38

2 answers

2

You can do it like this:

$(".download a").click(function(e) {
    var text = $(this).closest('.vei').next('.titulo').find('a').text();
    $(this).html(text);
});

jsFiddle: link

The idea is to climb the DOM with .closest('.vei') , search for the next element with .next('.titulo') and then go down inside this element looking for .find('a') .

    
03.10.2016 / 21:44
1

You put 2; (semicolon)

Replace

$( ".download a" ).click(function(e){
    $( ".download a" ).html($( ".titulo a" ).text(););
});

By

$( ".download a" ).click(function(e){
    $( ".download a" ).html($( ".titulo a" ).text());
    return false;
});
    
03.10.2016 / 21:47