Hi everyone im try loop in tags of html and when I press one button need that change the text of h2 ... but i dont know how to do.
this me html
let contenedor = document.querySelector('.container');
let chText = document.querySelector('.change');
contenedor.addEventListener('click', (e) => {
console.log(`Nombre: ${this.name} Nombre target: ${e.target.className}`);
if(e.target.matches('.inicio')){
changeText(e);
}
});
function changeText(e){
//console.log(e.currentTarget)
chText.innerText = 'text Change';
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="card" style="border:1px solid gray; width: 20%">
<h2 class="change">HIHO</h2>
<button class="inicio">Change</button>
</div>
<div class="card" style="border:1px solid gray; width: 20%">
<h2 class="change">HIHO</h2>
<button class="inicio">Change</button>
</div>
<div class="card" style="border:1px solid gray; width: 20%">
<h2 class="change">HIHO</h2>
<button class="inicio">Change</button>
</div>
</div>
</body>
</html>
If I use querySelectorAll to class change, try loop but this change all text.. and i want to do that only change text the button I press
how to referece between button and text (h2)..?