Select next elements

Asked

Viewed 211 times

5

I have a table with inputs

<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>

In the input with onchange, performs the function xxx().

What I’d like in the job is to get the next name inputs, in this case "CCC and DDD".

Taking advantage would like to know how to catch the previous one too ("AAA").

I tried to use the next(), nextAll(), find(), but I couldn’t.

var a = $(elementothis).next('input');
var a = $(elementothis).nextAll('input');
  • Actually you need to get the attr name of siblings... but also don’t know how to do :D

  • @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 elements input form, but I couldn’t position the selector on the element that comes from this, if it wasn’t just calling the next on the list... but it didn’t... rs

  • You’ll get with parents('tr').find('th').eq(3) then input. I’ll run some tests here.

  • @Laérciolopes does not have a more dynamic form?

  • this.parentElement.nextSibling.querySelector('input');

  • @Rbz, I don’t know any other...

  • @Daviaragao This is not jQuery right!? rs

  • is Javascript...

Show 3 more comments

2 answers

4


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.

  • Isac, the find I was trying to use it this way: I took all elements input from the form, generating a list. So I have all input and the context element (which came from this). If I could identify the context input on the list, next in the list, and could select the next one. The way you did it didn’t get dynamic because you locked the attribute name...

  • @Rbz But find allows you to find several. I gave an example with the name just to be simple, but when you finish lunch I put here another. basically . Closest("tr"). find("input") finds all list input and therefore all siblings.

  • Yes, he thinks everyone.. automatically I would have the list in sequence... as my function behind the element ( onchange=xxx(this) ), I could place the cursor in the list... then I would only give next... and put up to X value of how many inputs to jump... no?

  • @Rbz Yes finds all, and just access the correct element with eq for example. I’ve already edited my answer to give that example as well.

  • It worked, but I had to hold the position... because if I have tr with different quantities of td, it seeks wrong if lock position in the eq...

1

The problem is that the input are inside td.

To documentation of .next() says the method catches the immediate brother element to the informed on its selector.

A simple solution to the problem using Javascript would be like this:

this.parentElement.nextSibling.querySelector('input');

To access the input in td above looks like, need to change little thing:

this.parentElement.previousSibling.querySelector('input');

jQuery is Javascript.

  • Daviaragao, I know that jQuery is JS, but I would really like the solution in jQuery, not to get mixed... If there is no other way out, then yes I will have to use the JS...

  • 1

    It’s simple @Rbz, refactor your entire code taking jQuery :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.