I’m developing a chrome extension for manipulating HTML elements. I got a little problem. The element that I want to manipulate is without ID or ClassName, like this:
JavaScript
x
2
1
<div style="width: 400px">
2
I want to manipulate the width. But there is no identifier in the tag div. How can I manipulate that tag using javascript DOM?
Advertisement
Answer
You can use querySelector.
Here is a simple example:
JavaScript
1
4
1
//adjust selector to target your div (more info in docs)
2
var div = document.querySelector('div[style="width: 100px; background-color: green"]')
3
//change this width to your preference
4
div.style.width = "700px"
JavaScript
1
8
1
<!-- This is my condition -->
2
<div class="wrap">
3
<div style="width: 100px; background-color: green">
4
<h1 class="h1">
5
Hello World
6
</h1>
7
</div>
8
</div>