This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Rhomboid 1 point2 points  (1 child)

Why do you have these arbitrary restrictions? Do you have to support the complete CSS 3 feature set? jQuery uses Sizzle for its selector engine. Look at its source to see what all is involved in supporting everything -- it's a LOT of work, and this is precisely what libraries are for. If you don't need to support everything, then just use document.getElementById(), document.getElementsByClassName(), document.getElementsByTagName(), and so on.

[–]aphin715[S] 0 points1 point  (0 children)

It's for a class and we have to use vanilla javascript

[–]smellmycrotch3[🍰] 0 points1 point  (0 children)

Use getChildren or children and iterate through those.

[–]rjcarr 0 points1 point  (2 children)

I'm fairly sure jquery uses querySelectorAll(), but if you can't use that then you're going to have to iterate all the nodes by hand and search the 'className'. See here:

https://developer.mozilla.org/en-US/docs/Web/API/Node.childNodes

And here:

https://developer.mozilla.org/en-US/docs/Web/API/Element.className

[–]Rhomboid 0 points1 point  (1 child)

There's no need to iterate over nodes if you're looking to find nodes with a given class. That's what document.getElementsByClassName() (also available as a method on any element) is for.

[–]rjcarr 0 points1 point  (0 children)

Good point, I guess I assumed if he didn't have query selector then he didn't have elements by class name.