1

How can I combine both class names and element attributes in a document.querySelector() call.

let's say my element is as below:

<div role="button" class="fj2rt Gdrwg"></div>

I want to pass both class names and attribute in the document.querySelector() call for example:

let btn = document.querySelector('.fj2rt.Gdrwg [role="button"]';

1 Answer 1

5

No space needed between the role name and class name.

So document.querySelector('.fj2rt.Gdrwg [role="button"]'; should be replaced with document.querySelector('.fj2rt.Gdrwg[role="button"]';.

I have attached working example.

let btn = document.querySelector('.fj2rt.Gdrwg[role="button"]');
console.log(btn); 
<div role="button" class="fj2rt Gdrwg"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

Can you pls approve my question if it works well on you?
have to wait another 3 mins to do that according to stackoverflow :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.