0

Good afternoon, I'm a beginner, I'm having trouble getting information from a table that consumes data from an api.

    index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <script src="main.js"></script>
    <title>Document</title>
</head>
<body onload="apendeOnClick()">
    <table id="tablerow">
        <caption>Alien football stars</caption>
        <thead>
            <tr>
                <th>DATA</th>
                <th>TELEFONE</th>
                <th>RAMAL</th>
                <th>DESCRIÇÃO</th>
                <th>ACÕES</th>
            </tr>
        </thead>
        <tbody id="table_body">
                   
        </tbody>
    </table>

    <script>
        
    </script>
</body>
</html>

My code is this, it feeds the table normally, but I would like when the person clicks on the row button to display the information in the alert.

main.js
fetch("https://my-json-server.typicode.com/lfnoleto/api-fake/contact").then((data) => {
    return data.json()
}).then((Objectdata) => {
    //console.log(Objectdata[0].data)
    let tabelaData = ""
  

    Objectdata.map((values)=>{
       
        tabelaData += `<tr>
            <td>${values.data}</td>
            <td>${values.tefone}</td>
            <td>${values.ramal}</td>
            <td>${values.descricao}</td>
            <td><button id="butao" class="btn btn-success" ><i class="fa fa-phone"></button></td>
        </tr>`
        
    })

    document.getElementById("table_body").innerHTML = tabelaData
    innerHTML = tabelaData

    
})

function apendeOnClick(){

    const table = document.getElementById("tablerow")
    console.log(table)
    if (table) {
        for (var i = 0; i < table.rows.length; i++) {
          table.rows[i].childNodes[5].childNodes[1].onclick = function() {
            tableText(this);
          };
        }
    }
}

function tableText(tableRow) {
    let telefone = tableRow.childNodes[1].innerHTML;
    let ramal = tableRow.childNodes[2].innerHTML;
    let obj = {'telefone': telefone, 'ramal': ramal};
    console.log(obj);
  }

I hope that when the person clicks on the telephone icon, he displays the extension and telephone information

enter image description here

1 Answer 1

1

you can use data attribute to store extension info or other information.

<button id="butao" data-extension="123" class="btn btn-success" > 

in javascript :

const button = document.querySelector('button'); 

button.dataset.extention // "123"

a good references:

https://www.w3schools.com/tags/att_data-.asp

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.