Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
How to get the id of previous ul

I have a div with 4 seperate lists, I need to know the id of the list before the one i click on.

So lets say i click on ul2 i need an alert of ul1 id.

currently im getting the clicked id with:

  var clicked_id = this.id;

and ive tried the following to get the above ul

  var div_id2 = $(this).parents(".divtest").next(".ultest").attr("id");
  var div_id = $(this).closest('ul').find('.ultest').attr('id');
  var x = $(this).prev().attr('id');

simplification of layout:

  <div id="doesnt matter" class="divtest">
    <ul id="ul1" class="ultest">
      <li id="doesnt matter"></li>
      <li></li>
      <li></li>
    </ul>
    <ul id="ul2" class="ultest">
      <li></li>
      <li></li>
      <li></li>
    </ul>
    <ul id="ul3" class="ultest">
      <li></li>
      <li></li>
      <li></li>
    </ul>
    <ul id="ul4" class="ultest">
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use jQuery.prev:

$('ul').click(function() {
  let prev = $(this).prev('ul')
  console.log(prev.attr('id'))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="doesnt matter" class="divtest">
  <ul id="ul1" class="ultest">
    <li id="doesnt matter">item</li>
    <li>item</li>
    <li>item</li>
  </ul>
  <ul id="ul2" class="ultest">
    <li>item</li>
    <li>item</li>
    <li>item</li>
  </ul>
  <ul id="ul3" class="ultest">
    <li>item</li>
    <li>item</li>
    <li>item</li>
  </ul>
  <ul id="ul4" class="ultest">
    <li>item</li>
    <li>item</li>
    <li>item</li>
  </ul>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!