How to select one element from another

Asked

Viewed 172 times

5

<select class="form-control dropup bs-select-hidden" name="sys_nivel_acesso_id">
</select>
<div class="btn-group bootstrap-select form-control dropup">
  <button class="btn dropdown-toggle btn-default" data-toggle="dropdown" type="button" title="-----">
</div>

Hello, I would like a help to select via jQuery the element button from the element select by name sys_nivel_acesso_id with the intention of giving out Focus.

I’ve tried to:

  • $('select[name="sys_nivel_acesso_id"]').next('div button').focus()
  • $('select[name="sys_nivel_acesso_id"]').next('div').next('button').focus()

But it didn’t work.

  • But you want any change in select give the focus?

  • No, just a simple line of code to give the Focus. Example: if it were p any other element, $(elemento).focus(); Since the button has no name and no id, I want to pick it up by the select above.

  • 1

    $('select[name="sys_nivel_acesso_id"]').next().find('button') Enough for what I see in your HTML

2 answers

2

Do so:

$('select[name="sys_nivel_acesso_id"]').siblings('div').find('button');

Html:

  <select class="form-control dropup bs-select-hidden" name="sys_nivel_acesso_id"></select>
  <div class="btn-group bootstrap-select form-control dropup">
    <button class="btn dropdown-toggle btn-default" data-toggle="dropdown" type="button" title="-----">Text</button>
  </div>

2


If the div is just after the select then you can use the .next() which gives you a single element, directly after the select. You can use the .siblings() but it returns many elements and will worsen performance when used with the .find() because it will invoke it for every element found..

$('select[name="sys_nivel_acesso_id"]').next().find('button').focus();

jsFiddle: http://jsfiddle.net/ofb5j03v/

Browser other questions tagged

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