What method can be used to select the first

element inside a

?

  • div.getElementsByTagName('p')[0]
  • div.querySelector('p:first-child')
  • div.querySelector('p')
  • div.getElementByTagName('p')
You can use div.querySelector('p') to select the first

element inside a

. This method finds the first

element that is a descendant of div. Alternatively, you can use getElementsByTagName('p')[0], but querySelector is more flexible and widely used.

Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *