Populate multiple html input with table information

Asked

Viewed 28 times

-1

I have a JSP with table with multiple rows and two columns. I have ten input lines where I want to play cell 0 and cell 1 in item1 and descr1 of the first input. By clicking on the next row of the table I want to play cell 0 and cell 1 in the item2 and in the descr2 of the second input and so on. Only that the same row of the table is being loaded in the first and second input or item1 descr1 and item2 and descr2.

        {
            linha = 1;
            let tabela = document.getElementById("tabprd");
            
            if(tabela != null)
            {
                for(let i = 0; i < tabela.rows.length; i++)               
                {
                   tabela.rows[i].onclick = 
                        function()
                        {
                            if(linha === 1)
                            {alert("linha: " + linha);
                                document.getElementById("item1").value = this.cells[0].innerHTML;
                                document.getElementById("descr1").value = this.cells[1].innerHTML;
                                linha++;
                            }
                        
                            if(linha === 2)
                            {alert("linha: " + linha);
                                document.getElementById("item2").value = this.cells[0].innerHTML;
                                document.getElementById("descr2").value = this.cells[1].innerHTML;
                                linha++;
                            }                         
                        };
                }
            }
        }```
Esse é o javascript que uso para o load.
Atenciosamente,
Cezar Apulchro.

1 answer

0

Guys problem solved, nothing to research and study a little more. My solution:

        function loadCell()
        {
            
            let tabela = document.getElementById("tabprd");
            
            if(tabela != null)
            {
                for(let i = 0; i < tabela.rows.length; i++)               
                {
                   tabela.rows[i].onclick = 
                        function()
                        {
                            if(linha === 1)
                            {alert("linha: " + linha);
                                document.getElementById("item1").value = this.cells[0].innerHTML;
                                document.getElementById("descr1").value = this.cells[1].innerHTML;
                                linha++;
                                return linha;
                            }
                            else
                                if(linha === 2)
                                {alert("linha: " + linha);
                                    document.getElementById("item2").value = this.cells[0].innerHTML;
                                    document.getElementById("descr2").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 3)
                                {alert("linha: " + linha);
                                    document.getElementById("item3").value = this.cells[0].innerHTML;
                                    document.getElementById("descr3").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 4)
                                {alert("linha: " + linha);
                                    document.getElementById("item4").value = this.cells[0].innerHTML;
                                    document.getElementById("descr4").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 5)
                                {alert("linha: " + linha);
                                    document.getElementById("item5").value = this.cells[0].innerHTML;
                                    document.getElementById("descr5").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 6)
                                {alert("linha: " + linha);
                                    document.getElementById("item6").value = this.cells[0].innerHTML;
                                    document.getElementById("descr6").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 7)
                                {alert("linha: " + linha);
                                    document.getElementById("item7").value = this.cells[0].innerHTML;
                                    document.getElementById("descr7").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 8)
                                {alert("linha: " + linha);
                                    document.getElementById("item8").value = this.cells[0].innerHTML;
                                    document.getElementById("descr8").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 9)
                                {alert("linha: " + linha);
                                    document.getElementById("item9").value = this.cells[0].innerHTML;
                                    document.getElementById("descr9").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                            else
                                if(linha === 10)
                                {alert("linha: " + linha);
                                    document.getElementById("item10").value = this.cells[0].innerHTML;
                                    document.getElementById("descr10").value = this.cells[1].innerHTML;
                                    linha++;
                                    return linha;
                                }                         
                        };
                }
            }
        }
        ```
Obrigado.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.