2

I have one table with first numeric column:

<table id="toppings"  border="1" cellpadding="2">
   <tr id="id1">
      <td>3</td>
      <td>row12</td>
      <td>row13</td>
  </tr>
  <tr id="id2">
      <td>12</td>
      <td>row22</td>
      <td>row23</td>
  </tr>
  <tr id="id3">
      <td>15</td>
      <td>row32</td>
      <td>row33</td>
  </tr>
  <tr id="id4">
      <td>22</td>
      <td>row42</td>
      <td>row43</td>
 </tr>
 <tr id="id5">
      <td>23</td>
      <td>row52</td>
      <td>row53</td>
 </tr>
 <tr id="id6">
   <td>55</td>
   <td>row62</td>
   <td>row63</td>
 </tr>
</table>

How can I get first value/column of row on clicking row. I found following example doing the same.

http://www.vbforums.com/showthread.php?522104-HTML-table-on-click-event

But I don't want to add event to each row manually as my table is having billions of records. Please help me. How can I get the row value (first column) on clicking row?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Manish
  • 2,853
  • 11
  • 41
  • 78
  • 2
    You really should describe the user-story or the purpose your after. Which problem are you trying to solve. What you are describing here sounds confusing… – feeela Nov 10 '14 at 18:19
  • I hope it doesn't have "billions" of records that might take the browser a really long time to render... – brso05 Nov 10 '14 at 18:23
  • I have edited my question. – Manish Nov 10 '14 at 18:24
  • You could try using jQuery adding on onclick by class. Just give all your rows the same class then add onclick to that class using jquery... – brso05 Nov 10 '14 at 18:27
  • Add a single click listener to the table, find the clicked row within a loop by checking `.parentElement` from `e.target` and then changing the object to its `parentElement` untill a row is found (jQuery would make this simple, no need for looping). Finally read `innerHTML/textContent` from the first cell on the found row. – Teemu Nov 10 '14 at 18:28

4 Answers4

4
$("#toppings tr").click(function(){
   alert( $(this).children().closest("td").html());
});

fiddle: http://jsfiddle.net/k1ezbcrk/

briansol
  • 1,583
  • 1
  • 11
  • 33
0

example for one row could be

<tr id="id2">
      <td><a  href="#" onclick="getColumnValue(this)">12</a></td>
      <td>row22</td>
      <td>row23</td>
  </tr>

<script>
 function getColumnValue(elem){
  alert(elem.innerHTML);
 }
</script>
userDEV
  • 425
  • 4
  • 17
  • He said : "But i don't want to add event to each row manually as my table is having billions of records" – Milad Nov 10 '14 at 18:36
0
  $("#toppings tr").click(function(){
      alert( $(this).children().first().html());
  });
Milad
  • 22,895
  • 9
  • 62
  • 76
  • Can i convert alert( $(this).children().first().html()); as numerical variable as i need to pass this as parameter. – Manish Nov 11 '14 at 06:25
  • of course , the value is number , you can assign it to a variable and use it however you want – Milad Nov 11 '14 at 18:26
0

You could use event delegation

var table = document.getElementsByTagName('table')[0];

function getValue(){
  console.log(event.target.parentElement.firstElementChild.innerText);
}

table.addEventListener('click', getValue, false);
Community
  • 1
  • 1
skube
  • 4,813
  • 7
  • 47
  • 64