JavaScript jQuery selecting direct “parents”, not similar

While you've already posted your own answer, I thought I'd offer an alternative, in case you're interested:

// caching the selected element (selecting by its id):
var selected = $('#selected'),

// caching the regular expression, in case it might be
// be needed again later; this matches a serious of
// one or more (+) numbers (\d) at the end of the
// string ($):
    levelRegex = /\d+$/,

// finding the numbers at the end of the selected
// element's class attribute, and using parseInt()
// to convert that to a number in base-10:
    selectedLevel = parseInt(selected.attr('class').match(levelRegex), 10),

// caching the selected element's closest ancestor
// <tr> element:
    selectedParent = selected.closest('tr'),

// caching the <tr> elements before the selectedParent:
    rowsBefore = selectedParent.prevAll(),

// using map() to iterate over those elements and
// if their child <td> element has a class equal to
// 'lvl-' + (selectedLevel - 1)
// we first decrement selectedLevel, and then
// return the text of the current <tr> element;
// decrementing here means we can only ever
// retrieve the first instance of an element
// with a 'lower' level:
    pseudoParents = rowsBefore.map(function (i) {
        if ($(this).find('td').hasClass('lvl-' + (selectedLevel - 1))) {
            --selectedLevel;
            return this.textContent;
        }

// converting the 'map' into a native Array:
    }).get();

console.log(pseudoParents);
// ["14", "13", "10", "1"]

var selected = $('#selected'),
  levelRegex = /\d+$/,
  selectedLevel = parseInt(selected.attr('class').match(levelRegex), 10),
  selectedParent = selected.closest('tr'),
  rowsBefore = selectedParent.prevAll(),
  pseudoParents = rowsBefore.map(function(i) {
    if ($(this).find('td').hasClass('lvl-' + (selectedLevel - 1))) {
      --selectedLevel;
      return this.textContent.trim();
    }
  }).get();

// snippet logs to the bottom of the result panel:
snippet.log(pseudoParents);

// logs to the console (obviously):
console.log(pseudoParents);
// ["14", "13", "10", "1"]
.lvl-0 {
  padding-left: 10px;
}
.lvl-1 {
  padding-left: 30px;
}
.lvl-2 {
  padding-left: 50px;
}
.lvl-3 {
  padding-left: 70px;
}
.lvl-4 {
  padding-left: 90px;
}
.lvl-5 {
  padding-left: 110px;
}
body {
  color: #ffffff;
  background: #000000;
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="lvl-0">1</td>
  </tr>
  <tr>
    <td class="lvl-1">2</td>
  </tr>
  <tr>
    <td class="lvl-2">3</td>
  </tr>
  <tr>
    <td class="lvl-2">4</td>
  </tr>
  <tr>
    <td class="lvl-2">5</td>
  </tr>
  <tr>
    <td class="lvl-1">6</td>
  </tr>
  <tr>
    <td class="lvl-2">7</td>
  </tr>
  <tr>
    <td class="lvl-2">8</td>
  </tr>
  <tr>
    <td class="lvl-2">9</td>
  </tr>
  <tr>
    <td class="lvl-1">10</td>
  </tr>
  <tr>
    <td class="lvl-2">11</td>
  </tr>
  <tr>
    <td class="lvl-2">12</td>
  </tr>
  <tr>
    <td class="lvl-2">13</td>
  </tr>
  <tr>
    <td class="lvl-3">14</td>
  </tr>
  <tr>
    <td class="lvl-4">15</td>
  </tr>
  <tr>
    <td class="lvl-4" id="selected">16</td>
  </tr>
  <tr>
    <td class="lvl-3">17</td>
  </tr>
  <tr>
    <td class="lvl-1">18</td>
  </tr>
  <tr>
    <td class="lvl-2">19</td>
  </tr>
  <tr>
    <td class="lvl-2">20</td>
  </tr>
  <tr>
    <td class="lvl-2">21</td>
  </tr>
</table>
Answer:1

So I am trying to create a way to detect when the body resizes and then I grab the new height and send it through a postmessage. The not correctly functioning part is when I downsize the body ( when I ...

So I am trying to create a way to detect when the body resizes and then I grab the new height and send it through a postmessage. The not correctly functioning part is when I downsize the body ( when I ...

  1. window.body.scrollheight
  2. window.document.body.scrollheight
  3. window.scrollto(document.body.scrollheight 0)
  4. window.scrollto(0 document.body.scrollheight) not working
  5. window.scrollto(0 document.body.scrollheight) python
  6. window.scrollby(0 document.body.scrollheight)
  7. window.scroll(0 document.body.scrollheight)
  8. window.top.postmessage(document.body.scrollheight * )

I made a Character class shared by the client and server in the same js file. The server instanciates these characters and stocks them in a characterList object. I send it to the client with socket....

I made a Character class shared by the client and server in the same js file. The server instanciates these characters and stocks them in a characterList object. I send it to the client with socket....

I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...

I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...

  1. javascript class value change
  2. javascript classlist value
  3. javascript classname value
  4. javascript class value get
  5. javascript class value set
  6. javascript value to classic asp
  7. javascript value as class name
  8. javascript read class value
  9. javascript get class value of element
  10. javascript check class value
  11. javascript add class value
  12. javascript update class value
  13. javascript input class value
  14. javascript span class value
  15. javascript remove class value
  16. javascript if class value
  17. javascript html class value
  18. javascript div class value
  19. javascript td class value
  20. javascript pass class value

Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...

Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...

  1. react js without npm
  2. install react without npm
  3. use react without npm
  4. create react app without npm
  5. run react app without npm start