• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can I select an element by "role" or "data-test-id"?

Johna

Frontend Developer
Staff Team
Guardian
I'm trying to select an element, but the element doesn't have any class or id.

This is the element:
HTML:
<div data-test-id="giftWrap" role="dialog" aria-label="Hybrid banner gift wrap" style="position: fixed; z-index: 680; background: rgba(0, 0, 0, 0.65); transition: height 0.5s cubic-bezier(0.26, 0.87, 0.74, 0.93) 0s; backface-visibility: hidden; bottom: 0px; color: rgb(255, 255, 255); height: 100%; left: 0px; width: 100%;"></div>

I'm wondering if I can use something like document.getElementById(), but then instead get the element by role or data-test-id.


Thanks.
 
@OldMan Thanks, do you also know how to select this element to edit with css.

For example if I have this element:
HTML:
<div id="example">example<div>
I can type:
CSS:
#example {
    background: #FFFFFF;
}

So like can I do the same thing but for:
HTML:
<div data-test-id="giftWrap" role="dialog" aria-label="Hybrid banner gift wrap" style="position: fixed; z-index: 680; background: rgba(0, 0, 0, 0.65); transition: height 0.5s cubic-bezier(0.26, 0.87, 0.74, 0.93) 0s; backface-visibility: hidden; bottom: 0px; color: rgb(255, 255, 255); height: 100%; left: 0px; width: 100%;"></div>
so like:
CSS:
#giftWrap {
    background: #FFFFFF;
}


Thanks.
 

New Threads

Buy us a coffee!

300x250
Top Bottom