Search code examples
javascriptcss-selectorsselectors-api

How to get the second match with QuerySelector?


The following statement gives me the first element with the class titanic

element = document.querySelector('.titanic');

How would I retrieve the second element with the same class?


Solution

  • Use document.querySelectorAll

    document.querySelectorAll('.titanic')[1]