1

HTML

...
<a id="delete1" href="http://www.example.com">TEST</a>
<p>First</p>
<p>Second</p>
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>
<div id="hidden-qsd">123</div>
...

JS

var id = $('#delete1').nextUntil("div[id^='hidden']").next().last().attr('id');

I'd like to get the id of the closest "div" starting with "hidden" located after the link "#delete1".
This previous code is working but I think there is a simpler way to do it.

Spilarix
  • 1,418
  • 1
  • 13
  • 24

2 Answers2

2
$('#delete1').nextAll('[id^="hidden"]').attr('id')

nextAll() is enough

example jsbin: http://jsbin.com/usowej/3/edit

Note: if you have more than one element whose id is starting with hidden just use

$('#delete1').nextAll('[id^="hidden"]:first').attr('id')

to retrieve just the first occurence, see http://jsbin.com/usowej/4/edit

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • Yes, it's simpler, thank you. However, "nextAll" don't stop after the first result : it reads the whole DOM after the delete link, to find all occurences. So, we can maybe find a more efficient solution. – Spilarix Oct 02 '12 at 13:12
  • 1
    well if you have more than one element whose id is starting with hidden just use `$('#delete1').nextAll('[id^="hidden"]:first').attr('id')` using `:first` pseudoclass – Fabrizio Calderan Oct 02 '12 at 13:16
1

You can use nextAll(selector) to get the next siblings after the element:

var id = $('#delete1').nextAll("div[id^='hidden']").prop('id');

http://api.jquery.com/nextAll/

Bogdan
  • 43,166
  • 12
  • 128
  • 129