Change the width of a Div by selecting the Div attribute

I have the following Div, which have an attribute named data-automation-id:-

enter image description here

now i want using javascript to set the width of this Div to be 50% then back to 75% instead of 100%? but i am not sure how i can select a Div using an attribute?

Answer

You can do this :

document.querySelector("div[data-automation-id='CanvasLayout']").style.width="50%";

Or with Jquery:

$("div[data-automation-id='CanvasLayout']").css("width","50%");

https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

Leave a Reply

Your email address will not be published. Required fields are marked *