I am creating a table depending on the result of a function for then append in a div like this:
let result = document.getElementById("result");
consultCities();
function consultCities (){
consultarAPI(username, password, "cities")
.then((response) => {
if(response.login !== "Fail"){
let table = "";
let thead = "";
let tbody = "";
thead += `<tr>
<th>Citie</th>
<th>Dane</th>
<th>Look Institution</th>
</tr>`
for(let i = 0; i < response.data.length; i += 1 ){
tbody += `<tr>
<td>${response.data[i].name}</td>
<td>${response.data[i].dane}</td>
<td><button type="button" id="ver" onclick="${consultInstitutions(response.data[i].dane)}">Ver</button></td>
</tr>`
}
table += `<table class="table table-bordered">
<thead>
${thead}
</thead>
<tbody>
${tbody}
</tbody>
</table>`
result.innerHTML = table;
}else{
alert("El Usuario no existe o la opciĆ³n no existe");
}
});
}
function consultInstitutions(codCity){
console.log(codCity);
}
The problem is the onclick trigger runs automatically although I don’t click in any button, my question is there a best way that I can set onclick a button for a string?