Choose the last id in div block



  • There's a structure.

    <div id="block">
      <p id="p_1">text</p>
      <p id="p_2">text</p>
      <p id="p_3">text</p>
      <p id="p_4">text</p>
      ....
    </div>
    

    We've got to pull out the last id in the example of p-4, how do we do it?



  • .last() - Reset the last element from all the chosen ones.

    http://jquery.page2page.ru/index.php5/%D0%9F%D0%BE%D0%B8%D1%81%D0%BA_%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%D0%B4%D0%BD%D0%B5%D0%B3%D0%BE_%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D0%B0_%D0%B2_%D0%BD%D0%B0%D0%B1%D0%BE%D1%80%D0%B5

    https://api.jquery.com/last/

    alert($('#block p').last().attr('id'));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="block">
      <p id="p_1">text</p>
      <p id="p_2">text</p>
      <p id="p_3">text</p>
      <p id="p_4">text</p>
    </div>

    Another option:

    :last-child - Consistent with all elements that are the last daughters of the parent.

    https://api.jquery.com/last-child-selector/

    http://jquery-docs.ru/Selectors/lastChild/

    alert($('#block p:last-child').attr('id'));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="block">
          <p id="p_1">text</p>
          <p id="p_2">text</p>
          <p id="p_3">text</p>
          <p id="p_4">text</p>
        </div>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2