Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native Javascript querySelectorAll() with multiple/many pseudo selectors/matches

How can I place many pseudo selectors inside the native Javascript querySelectorAll()?

Example: I want to search for an element with an id that starts with [id^=starting] and ends with [id$=ending]. (Couldn't find existing question so making my own and answering it)

like image 270
goamn Avatar asked Oct 19 '25 01:10

goamn


1 Answers

With Native Javascript this would be the code:

document.querySelectorAll('[id^=starting][id$=ending]');

or

document.querySelectorAll('[id^='+startingString+'][id$='+endingString+']');

This will get an element which starts with the specified string AND ends with the specified string.

Edit: And to do an "OR", put a space between them:

document.querySelectorAll('[id^=starting] [id$=ending]');

like image 125
goamn Avatar answered Oct 21 '25 15:10

goamn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!