JSFiddle - React, Tailwind, and code Playground

HTML

<input class="click" type="button" value="1">
<p class = "error">first</p>
<table>
<tr><td><input class="click" type="button" value="2"></td></tr>
</table>
<p class="error">second</p>

JavaScript

const $ps = $('p');
const allElms = $('*');
$(".click").click(function() {
  var val = this.value
  const thisElmIndex = allElms.index(this);
  const followingElements = allElms.slice(thisElmIndex + 1);
  alert(followingElements.filter('p')[0].textContent)
  // followingElements.filter('p')[0].value = val
});