Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use querySelector to get select based on two conditions, one being a prefix

So I am trying to select a div based on two conditions, one being a prefix.

So if I have

<div class="bob delay-10"></div>
<div class="joe delay-20"></div>

I want to have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function getdelay (classname) {
    let delayselect = document.querySelector("*[class='"+classname+"'], *[class*='delay-']");
return delayselect;
}
getdelay("joe");

should return <div class="joe delay-20"></div>

Currently the way it is working is it is selecting OR not AND.

How can I fix this?

>Solution :

Use an actual class selector (.) for the fixed class name part.

let delayselect = document.querySelector(`.${classname}[class*='delay-']`);

The backticks ("template literals") allow for simpler inclusion of expressions in strings.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading