The same is for JS only. How to translate?

$(document).ready(function(){ $("#form_error_message_frontend + div > div:last-child label").addClass("last"); }) 

4 answers 4

Something like this:

 document.addEventListener('DOMContentLoaded', function(){ // Аналог $(document).ready(function(){ // Если должен быть найден один элемент if((e = document.querySelector("#form_error_message_frontend + div > div:last-child label")) !== null) e.classList.add('last'); // Аналог выборки и присвоения класса // Если элементов будет много Array.prototype.forEach.call(document.querySelectorAll("#form_error_message_frontend + div > div:last-child label"), function(e){ e.classList.add('last'); }); }); 
  • It does not querySelector , but a querySelectorAll plus an iteration over the returned NodeList . - VisioN
  • @VisioN, the last class unambiguously hints that there is only one element, which means that you can search through querySelector . Although if the TS is wrong, then rewrite :) - user31688
  • Not quite so :) Although it may be contrary to common sense, there may be several label elements in the last block. In this case, purely logically, the added class "last" may refer not to the last label , but to all label in the last block. - VisioN
  • I know, my friend, but I hoped for the author's common sense :) If the answer is accepted, then the meaning is in place, if not, we will rewrite it into a cycle, business ... - user31688
  • Well, let's hope :) But then, so that the querySelector does not work out, for querySelector you can add a check for the presence of the returned element before accessing the classList property: well, how will undefined return? - VisioN

good answer is here

90 +% of browsers will cover this solution to wait for the download to finish.

 document.addEventListener("DOMContentLoaded", function(event) { //do work }); 

for 95 +% of browsers, you can use the methods querySelector / querySelectorAll

 var el = document.querySelector("#form_error_message_frontend + div > div:last-child label"); el.className += el.className ? ' last' : 'last' 

    Perhaps you will only need to add deferred script loading with the "defer" attribute to <script> So the script will wait for the entire DOM tree to load, and then it will start.

    • Can proof? The first time I hear about this ... - user273805
    • It remains to understand what "page has loaded" means. Because you do not need to wait for the uploading of pictures to start the script. - Evgeni Nabokov
    • I think this happens after DOMContentLoaded, that is, uploading pictures and other things is not taken into account, only the DOM tree. - Anton Golova 1:22 pm

     <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Пример</title> <script language="JavaScript"> function startFunction() { alert("Hi there!"); } </script> </head> <body onload="startFunction()"> </body> </html>