As explained above, the <input>
where the code breaks, there are no brothers, and he’s alone in the <td>
, soon enough next
, as nextAll
or find
can’t find anything. You need to go first to the <td>
(father), then sail to another <td>
and access your child.
You can do this with parent
, next
, children
and first
function xxx(elem){
$(elem).parent().next().children().first().val("150");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Linha X</th>
<td><input type="number" name="AAA"></td>
<td><input type="number" name="BBB" onchange="xxx(this)"></td>
<td><input type="number" name="CCC" disabled></td>
<td><input type="number" name="DDD" disabled></td>
</tr>
</table>
Or more dynamically with closest
and find
:
function xxx(elem){
$(elem).closest("tr").find("[name=CCC]").val("150");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Linha X</th>
<td><input type="number" name="AAA"></td>
<td><input type="number" name="BBB" onchange="xxx(this)"></td>
<td><input type="number" name="CCC" disabled></td>
<td><input type="number" name="DDD" disabled></td>
</tr>
</table>
This form is more dynamic and makes html more flexible, in addition to being simpler as well.
The closest
navigate up to the <tr>
, and the find
navigate down to the element that matters according to the specified selector.
If you need to access several, it is best to use a generic selector on find
and get the array representing the captured elements:
function xxx(elem){
const inputs = $(elem).closest("tr").find("input");
inputs.eq(0).val("100"); //AAA
inputs.eq(2).val("150"); //CCC
inputs.eq(3).val("200"); //DDD
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Linha X</th>
<td><input type="number" name="AAA"></td>
<td><input type="number" name="BBB" onchange="xxx(this)"></td>
<td><input type="number" name="CCC" disabled></td>
<td><input type="number" name="DDD" disabled></td>
</tr>
</table>
Access to position is given by the method eq
. Remember that with this selector captures all <input>
including the event itself.
Actually you need to get the attr name of siblings... but also don’t know how to do :D
– hugocsl
@hugocsl I can’t reach the element rs... I think if I get to it, the rest is quiet. By
find()
I can find all the elementsinput
form, but I couldn’t position the selector on the element that comes fromthis
, if it wasn’t just calling the next on the list... but it didn’t... rs– rbz
You’ll get with
parents('tr').find('th').eq(3)
then input. I’ll run some tests here.– Laércio Lopes
@Laérciolopes does not have a more dynamic form?
– rbz
this.parentElement.nextSibling.querySelector('input');
– DaviAragao
@Rbz, I don’t know any other...
– Laércio Lopes
@Daviaragao This is not jQuery right!? rs
– rbz
is Javascript...
– DaviAragao