how to get values of selection row in html table and show them in a input text using javasricpt?

How can I click on an HTML row then show the selection values of the row in input text?

I've tried like this, but what shows up in the input text only the first row even though I click the second row and so on..

div> <button type="button">...</button> <!-- Modal --> <div tabindex="-1" aria-labelledby="myModalLabel" scrollY:380> <div> <div> <div> <button type="button" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4>Category</h4> </div> <div> <table cellspacing="0" width="100%"> <thead> <tr> <th>Id Event Category</th> <th>Event Category</th> <th>Description</th> </tr> </thead> <tbody> <% objLOV.forEach(function (lov) { %> <tr> <td onclick="myFunction()"> <%= lov.id %> </td> <td onclick="myFunction()"> <%= lov.event_category %> </td> <td onclick="myFunction()"> <%= lov.description %> </td> </tr> <% }) %> </tbody> </table> 

and my javascript

<script> function myFunction() { document.getElementById("id_type").value = document.getElementById("field_id").textContent.trim(); document.getElementById("event_category").value = document.getElementById("field_category").textContent.trim(); document.getElementById("description").value = document.getElementById("field_description").textContent.trim(); } </script> 

"id_type" , "event_category" , "description" is my input text type id

2

2 Answers

Apply the click event for <tr> and pass the current reference this to the calling function like <tr onclick="callme(this)">. From the javascript get the current row reference and find all the td inside that. Now get the values using innerHTML and assign it to the respective input fields("id_type" , "event_category" , "description"). Look at the following example.

function callme(e) { var tds=e.getElementsByTagName('td'); document.getElementById("id_type").value = tds[0].innerHTML.trim(); document.getElementById("event_category").value = tds[1].innerHTML.trim(); document.getElementById("description").value = tds[2].innerHTML.trim(); }
<table> <tr onclick="callme(this)"> <td>test1</td> <td>something1</td> <td>content1</td> </tr> <tr onclick="callme(this)"> <td>test2</td> <td>something2</td> <td>content2</td> </tr> <tr onclick="callme(this)"> <td>test3</td> <td>something3</td> <td>content3</td> </tr> </table> <input type="text" /> <input type="text" /> <input type="text" />

Note: As per my comment, don't use the same id for all your td. You can try to use class instead of td. For this current solution it is not affecting but in feature it will give you the wrong information as like your code. It is important id should be unique.

0

According to HTML spec id attribute should be unique in a page,

so if you have multiple elements with same id, your HTML is not valid.

getElementById() should only ever return one element. You can't make it return multiple elements.

So you can use unique id for each row or try using class

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like