juunichiro
juunichiro

Reputation: 47

Get multiple child div texts on button click inside of parent div

I have multiple divs that have 3 div children each, 2 with the classes: name and email, and a button.

When any button inside the parent div is clicked, the text of the other children should be assigned to other 2 different divs with the ids #name and #email.

Something like this:

div1{name: john, email: [email protected], button}
div2{name: karen, email: [email protected], button}

#name: --
#email: --

When I click div1's child button this should happen:

div1{name: john, email: [email protected], button}
div2{name: karen, email: [email protected], button}

#name: john
#email: [email protected]

If possible, I would like a way to do it via javascript or jquery since I'm a begginer...

Thanks in advance!

Upvotes: 0

Views: 183

Answers (1)

Pranav Rustagi
Pranav Rustagi

Reputation: 2721

Do this :

$('div > button').on('click', function(){
  let emailVal = $(this).siblings('.email').text();
  $('#email').text(emailVal);
  let nameVal = $(this).siblings('.name').text();
  $('#name').text(nameVal);
});

Upvotes: 0

Related Questions