It is necessary that the contents of the html list element be displayed in the alert js window. But, the code below does not work. Tell me, pliz, errors:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript"> function Click(evt) { alert(evt.target.ownerDocument.getElementByTagName('li').innerHTML); { </script> </head> <body> <ul> <li onclick = "Click(evt);">Первый</li> <li onclick = "Click(evt);">Второй</li> </ul> </body> </html> 

    3 answers 3

    Well, let's start with the fact that the closing bracket of the Click function is in the opposite direction.

     <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript"> function Click(obj) { alert(obj.innerHTML); } </script> </head> <body> <ul> <li onclick = "Click(this);">Первый</li> <li onclick = "Click(this);">Второй</li> </ul> </body> </html> 

    Here it will work.

      1. The function body is not closed, but open again.
      2. You are trying to pass an event to a function, but an element is actually passed.
      3. evt.target.ownerDocument - fierce ....
      4. getElementByTagName () - there is no such function. There is a getElementsByTagName ().
      5. But it returns the nodelist, and not one element, so it will not work to get its innerHTML.
      6. You climb into the wilds of the language without reading the basics.

        Do not properly handle getElementByTagName! That's right - getElementsByTagName! Elements - since there are a lot of elements, do not compare with getElementById - here the element (Element) is only 1 And then, getElementsByTagName ('li') [0] - returns the first li, [1] - the second, etc.