Search code examples
javascriptcss-selectorsjquery-selectors

Is there a way to use :first in javascript queryselector?


I have a table containing a list of users and I would like to hide all their duplicates on an event. an example in the image below is hiding the second tr#user-3.

enter image description here

But when I tried using temp1.querySelectorAll('tr:not(#user-3:first)') it didn't return any node instead and it brings invalid selector error. What could be the cause and how do I solve it ?

enter image description here


Solution

  • :first is not part of the CSS spec, this is jQuery specific.

    To access the first and last elements, try.

        var nodes = temp1.querySelectorAll('tr');
        var first = nodes[0];
        var last = nodes[nodes.length- 1];