JSFiddle - React, Tailwind, and code Playground

HTML

invalid: (<span id="log1"></span>)(<span id="log2"></span>)
<table>
    <form name="f1">
        <tr>
            <td>some text</td>
            <td><input type="text" id="myid1"></td>
        </tr>
        <tr>
            <td>another text</td>
            <td><input type="submit"></td>
        </tr>
    </form>
    <form name="f2">
        <tr>
            <td>te</td>
            <td><input type="text" id="myid2"></td>
        </tr>
        <tr>
            <td>and more</td>
            <td><input type="submit"></td>
        </tr>
    </form>
</table>

<br><br>

valid: (<span id="log3"></span>)(<span id="log4"></span>)
<form name="f3">
<table>
    <tr>
        <td>some text</td>
        <td><input type="text" id="myid3"></td>
    </tr>
    <tr>
        <td>another text</td>
        <td><input type="submit"></td>
    </tr>
</table>
</form>    
<form name="f4">
<table>
    <tr>
        <td>te</td>
        <td><input type="text" id="myid4"></td>
    </tr>
    <tr>
        <td>and more</td>
        <td><input type="submit"></td>
    </tr>
</table>
</form>
    
<input type="button" id="run" value="run">

JavaScript

$('#run').click(function(){
    debugger;
    $('#log1').html($('#myid1').closest('form').attr('name'))
    $('#log2').html($('#myid2').closest('form').attr('name'))
    $('#log3').html($('#myid3').closest('form').attr('name'))
    $('#log4').html($('#myid4').closest('form').attr('name'))    
});