Tell me how to get the id of the form? been here like that doesn't come out

console.log($("form").attr("id")); 

Throws undefined

Everything works on Yii2 js scripts at the end of the body

1 answer 1

Try this:

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> window.onload = function() { console.log($("form").attr("id")); } </script> </head> <body> <form id="test_form"> </form> </body> </html> 

  • Tell me if the js code goes at the end of the html file? how to be in this situation? If I try to get an id by class for example or vice versa, everything works. - Maxim Sychevsky
  • one
    Try at the end of the file, it should work. - Dodik
  • Strange but it was your code that launched it in a separate file and it works. Maybe Yii somewhere gives some restrictions. - Maxim Sychevsky
  • Found a mistake. On the page was another form of logout, in which the adishnik is not set! Found on such code $ ("form"). Attr ("id", "pidor"); after execution I searched in ihodnik for context)))) - Maxim Sychevsky