deepak randhawa
deepak randhawa

Reputation: 111

how to get values from table cell dynamically in javascript

This is my code and i am trying to get values by onclick form a table cells dynamically.

let Table = document.querySelector('#recieve-info');
Table.addEventListener('click',(e)=>{     
     if(e.target && e.target.classList.contains('Save-Button')){
        let tr = document.querySelector('tr');
        console.log(tr.firstElementChild);
    }
});

Here is the output in console.log i am getting only the first value of table cell

Upvotes: 1

Views: 697

Answers (1)

Codebling
Codebling

Reputation: 11382

.querySelector() only returns the first matching element.

Use .querySelectorAll(), which will return an array of all matching elements. You'll then have to iterate through the array.

        let trs = document.querySelectorAll('tr'); //**** <- note "All" added
        trs.forEach(tr => console.log(tr.firstElementChild));

Upvotes: 2

Related Questions