GK Question

technology medium mcq

Which JavaScript method is used to select an HTML element by its ID?

  1. querySelector()
  2. getElementById()
  3. getElementsByClassName()
  4. getElementsByTagName()

Answer: getElementById()

getElementById('id') returns the element with specified ID (case-sensitive). querySelector() uses CSS selectors for more flexibility. getElementsByClassName/TagName return collections. DOM manipulation is core to interactive web development and frequently tested in programming sections of exams.

Topic Web Technologies
Exam Relevance SSC JE, Banking IT, Railway