Hello, there is a div and there are three more divs in it, I get the parent id and write it to the key2 variable, I can’t refer to the child element in this situation:

var key2 = snap.key; //тут получаю id родительского элемента $('#' + key2 :last-child) // как обратиться к последнему дочернему элементу 
  • Documentation - Mr. Black
  • It is worth adding html, to which the script is applied with an indication of exactly which element to get - Grundy

1 answer 1

The code from the question is quite working, if you correct the error of collecting the string.

 var key2 = 'container'; //тут получаю id родительского элемента $(`#${key2} :last-child`).html('я последний'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>