JavaScript Retrieve parent element ID on focus out

I'm trying to retrieve the TR element's ID, whenever the user removes focus from that TR. Triggering the event is easy enough, but I can't work out how best to retrieve the ID

$(".row-identifying-class").focusout(function (e) {
    var rowID = e.target.id; // Returns the ID of the TD/input/label etc the user last clicked on
    alert(e.target);
})

The problem appears to be that although the function above triggers whenever I remove focus from the TR, the event is actually triggered by the child element (eg the TD, input box, button etc), not the parent element.

Is there any way to retrieve the original TR, without having to trace back through the parent of each object until I hit a TR element? Perhaps by passing the ID in directly when creating the function?

Answer:1

You're correct in that e.target will refer to the element that raised the new event which caused the focusout to fire on the tr.

Instead, use the currentTarget property as that will point to the event bound element instead, or more simply just this.id:

$(".row-identifying-class").focusout(function(e) {
  console.log(e.currentTarget.id);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="row-identifying-class" id="tr">
    <td>
      <input type="text" value="cause a focusout on me..." />
    </td>
  </tr>
</table>
Answer:2

Am trying to use fitBounds to make all my markers visible with a proper zoom level. getBounds(){ return [[12.49637,41.90278],[12.319398,45.441906],[13.055054,47.809532],[16.373724,48.208244]] } &...

Am trying to use fitBounds to make all my markers visible with a proper zoom level. getBounds(){ return [[12.49637,41.90278],[12.319398,45.441906],[13.055054,47.809532],[16.373724,48.208244]] } &...

This seems fairly simple but I can't get this to run. I basically want to check if 'div class = balances' contains the text 'EUR' and if it does, I want to do other checks. It works when I use an ...

This seems fairly simple but I can't get this to run. I basically want to check if 'div class = balances' contains the text 'EUR' and if it does, I want to do other checks. It works when I use an ...

  1. if statement protractor element

I'm trying to remove a specific object when a property matches my parameter (Here when the name of the wheel is "Michelin") but I can't make it work... How could I do it ? var car = { type: "...

I'm trying to remove a specific object when a property matches my parameter (Here when the name of the wheel is "Michelin") but I can't make it work... How could I do it ? var car = { type: "...

  1. remove nested object javascript
  2. lodash remove nested object
  3. mongodb remove nested object
  4. mongoose remove nested object
  5. remove nested json object
  6. remove nested property from object javascript
  7. nested object remove

I want to create a reusable component where the DOM structure can be different each time the component is rendered. Let's say I have this class Comp extends React.Component { constructor() { ...

I want to create a reusable component where the DOM structure can be different each time the component is rendered. Let's say I have this class Comp extends React.Component { constructor() { ...

  1. react render sub component
  2. react force render subcomponent