JavaScript Get id from html table when clicking an input inside the table from html table to csv,from html table to json,

How about this:-

Demo

Html

<table id="tblTest">
    <tr>
        <td>
            <input type="text" id="txtTest" onclick="getParent.call(this)" />
        </td>
    </tr>
</table>

I am using call here so that i get the elements context inside the getParent event callback.

Javascript

function getParent()
{
  var parent = this.parentNode;
  var tagName = "table";

  while (parent) { //Loop through until you find the desired parent tag name
  if (parent.tagName && parent .tagName.toLowerCase() == tagName) {
      alert(parent .id);
      return;
    }
      else
      {
          parent = parent .parentNode;
      }

  }

}

If you are using Jquery:-

in the click event you can just do $(this).closest('table').attr('id')

Answer:1

If you are using jQuery you can use closest to find the closest matching ancestor like so:

var tableID = "";
$(document).ready(function(){
    $('input[type="text"]').click(function(e){
        tableID = $(this).closest('table').attr('id');
    });
]);

Edit:

If you actually want to do something with that table (for instance add a class), you could do the following:

$(document).ready(function(){
    $('input[type="text"]').click(function(e){
        tableID = $(this).closest('table').addClass('myClass');
    });
]);

This simply removes the need to fetch the table ID, store it, and then fetch the table based on it's ID. Since you already found the table in order to get its ID you can just manipulate it right away.

Answer:2

I have a json like so: { "a": { "x": { "y": { "a": {}, "z": {}, "b": {} } }, "c": {}, "b": { "c": { "d": {} ...

I have a json like so: { "a": { "x": { "y": { "a": {}, "z": {}, "b": {} } }, "c": {}, "b": { "c": { "d": {} ...

pls can somebody give the date validation regex, which will allow the following rules are It should allow mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, m/d/yyyy (not allow yy) Number of days for month (30 and 31)...

pls can somebody give the date validation regex, which will allow the following rules are It should allow mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, m/d/yyyy (not allow yy) Number of days for month (30 and 31)...

  1. regex date validation mm/dd/yyyy
  2. regex date validation javascript
  3. regex date validation leap year
  4. regex date validation yyyy-mm-dd
  5. regex date validation dd/mm/yyyy
  6. regex date validation
  7. regex date validation java
  8. regex date validation c#
  9. regex date validation jquery
  10. regex date validation example
  11. regex validation date format
  12. regex validation date php
  13. regex validate date format dd/mm/yyyy
  14. regex validate date of birth
  15. regex validate date string
  16. regex validate date time
  17. regex validate date jsf
  18. javascript regex date validation yyyy-mm-dd
  19. js regex date validation
  20. python regex date validation

I have a rails app (on the dev server) and the application.js file is not loaded from the browser. When I make a direct call to the application.js file from the browser ex: myapp.com/assets/...

I have a rails app (on the dev server) and the application.js file is not loaded from the browser. When I make a direct call to the application.js file from the browser ex: myapp.com/assets/...

I am trying to push a value to a property of an array like below var obj = {}; obj.a = (obj.a || []).push(10); console.log( typeof obj.a ); // Returning number How can correct this to keep ...

I am trying to push a value to a property of an array like below var obj = {}; obj.a = (obj.a || []).push(10); console.log( typeof obj.a ); // Returning number How can correct this to keep ...